Permalink
Browse files

add docs for reduce

  • Loading branch information...
1 parent e4696c1 commit 1dd39884cacdc275b9e1d38d9884297c2677ba05 Robey Pointer committed Jun 12, 2012
Showing with 12 additions and 0 deletions.
  1. +12 −0 README.md
View
@@ -101,6 +101,8 @@ each parser has methods on it, also, to allow for combining:
- `repeat(atLeast, sep)` - just like `parser.repeat`
+- `reduce(sep, function)` - a simpler variant of `foldLeft` (see below)
+
- `drop()` - if this parser matches, return null as the match result, which
will cause it to be omitted from the result of `parser.seq`
@@ -133,6 +135,16 @@ for example, to match a sequence of numbers separated by "+" and add them:
fold: (sum, op, n) -> sum + n
)
+this is aliased to "reduce" on Parser, with a simplified interface:
+
+ number = parser.regex(/\d+/).onMatch (m) -> parseInt(m[0])
+ expr = parser.foldLeft(
+ tail: number
+ sep: parser.string("+")
+ accumulator: (n) -> n
+ fold: (sum, op, n) -> sum + n
+ )
+
implicit conversion
-------------------

0 comments on commit 1dd3988

Please sign in to comment.