I’m trying to make the following, from a dynamically filled List:
val primitives = "x" | "y" | "z" // what I want
val primitives2 = List("x", "y", "z") // what I need to transform from
I figured something like this might work:
primitives2.reduce(_|_)
But no go. I then found this snippet, which works:
primitives2.foldRight(failure("no matching delimiter"): Parser[Any])(_|_)
However, the base case failure("no matching delimiter") is confusing. Is that just the equivalent Nil case for Parser objects?
I’m going to assume that you’re working with
RegexParsersor one of its descendants. If so, then the issue is just that the implicit conversion fromStringtoParser[String]won’t kick in automatically withreduce(_ | _). If you explicitly convert every item in your list first, like this:You’ll be perfectly fine—except that this will leave you with slightly confusing error messages, like this:
If you want a clearer error message, then you’ll need to provide your own starting value using the
foldapproach.