pchiusano avatar pchiusano committed 194c960

added modifyUser combinators

Comments (0)

Files changed (1)

src/main/scala/nomo/Parser.scala

     def >>[B](p: => Parser[B]): Parser[B] = (this map2 p)((_,b) => b)
     def >>![B](p: => Parser[B]): Parser[B] = this.commit >> p 
 
+    /** Adds the result of this parser to the user state using the provided function 
+      * before returning. */
+    def modifyUser(f: (A, U) => U): Parser[A] = 
+      this flatMap (a => Parsers.this.modifyUser(f.curried(a)) as a)
+
     /** Ignore the result of this parser. */
     def ignore: Parser[Unit] = this >> unit(())
 
   def setUser(u: U): Parser[Unit] = Cont((i, a) =>
     { val na = a.setUser(u) ; suspendS { (Done(Success(()), IndexedSeq(i), na), na) }})
 
+  /** Parser which consumes no input and modifies the user state using the given function. */
+  def modifyUser(f: U => U): Parser[Unit] = 
+    getUser flatMap (setUser(_))
+
   /** Parser which consumes no input and always returns the given A. */
   def unit[A](a: A): Parser[A] = {
     val p = Cont((i, ann) => suspendS { (Done(Success(a), IndexedSeq(i), ann), ann) })
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.