Search code examples
scalaparser-combinators

Always discard result for a parser combinator


In earlier versions of scala the discard method existed to throw away the results of a parser:

lazy val throwThisAway: Parser[String] = (ows ~> discard(comma | EOF | EOL)) <~ ows

How may that be achieved in current versions of the library .. i.e while simply doing

otherParser ~ throwThisAway ~ anotherParser ^^ { case a ~ b // only 2, not 3, parser results

Solution

  • You can simply reference the extra parser result but not use it:

    otherParser ~ throwThisAway ~ anotherParser ^^ { case a ~ x ~ b => // eg.: SomeCaseClass(a,b)
    

    or even:

    otherParser ~ throwThisAway ~ anotherParser ^^ { case a ~ _ ~ b => ...