2

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.