Can someone explain how and when to use the triple caret ^^^ (vs the double caret ^^) when designing scala parser combinators? And also when / how to use the parser.into() method (>>).
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
- Error in Scala Compiler: java.lang.AssertionError:
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- GRPC: make high-throughput client in Java/Scala
- Setting up multiple test folders in a SBT project
- Testing request with CSRF Token in Play framework
- Run project with java options via sbt
I'll begin with an example using Scala's
Option
type, which is similar in some important ways toParser
, but can be easier to reason about. Suppose we have the following two values:Option
is monadic, which means (in part) that we canmap
a function over its contents:It's not uncommon to care only about whether the
Option
is full or not, in which case we can usemap
with a function that ignores its argument:The fact that
Option
is monadic also means that we can apply to anOption[A]
a function that takes anA
and returns anOption[B]
and get anOption[B]
. For this example I'll use a function that attempts to parse a string into an integer:Now we can write the following:
This is all relevant to your question because
Parser
is also monadic, and it hasmap
andflatMap
methods that work in very similar ways to the ones onOption
. It also has a bunch of confusing operators (which I've ranted about before), including the ones you mention, and these operators are just aliases formap
andflatMap
:So for example you could write the following:
Where each parser behaves in a way that's equivalent to one of the
Option
examples above.