Here is what I am trying to do:
def parser = parser_a >> {
case a => val c = compute(a) ; parser_b(c)
} ^^ {
case a ~ b => (a, b)
}
Of course it won't work, since the function after the ^^
operator only gets the result of parser_b
. How can I keep the result of parser_a
?
You can use the fact that parsers are monadic to write this as follows:
val parser = for {
a <- parser_a
b <- parser_b(compute(a))
} yield (a, b)
Alternatively you could change the following line in your solution (note that success
here is just a less specific version of the general monadic return
).
case a => val c = compute(a) ; success(a) ~ parser_b(c)
I personally find the for
-comprehension a little clearer in this case, though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.