summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-04-29 00:40:14 -0700
committerDavid Barksdale <amatus.amongus@gmail.com>2010-04-29 00:40:14 -0700
commit25bee68cd6c8897b41aec35e93aef18a1f0cb929 (patch)
tree42e21e3ddac179b23905d87dcd980f154611c6a5
parent65f36dadaf0b218f52f35ec996b9bd5468cf8ba6 (diff)
The beginning of the parser monads.
-rw-r--r--src/org/gnu/clojure/gnunet/parser.clj18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/org/gnu/clojure/gnunet/parser.clj b/src/org/gnu/clojure/gnunet/parser.clj
new file mode 100644
index 0000000..c89876a
--- /dev/null
+++ b/src/org/gnu/clojure/gnunet/parser.clj
@@ -0,0 +1,18 @@
+(ns org.gnu.clojure.gnunet.parser
+ (:use clojure.contrib.monads))
+
+(def parser-m (state-t maybe-m))
+
+(defn item
+ "Parser which returns the first item of input."
+ [xs]
+ (when (not (empty? xs)) [(first xs) (rest xs)]))
+
+(defn items
+ "Produces a parser that returns the first n items of input."
+ [n]
+ (with-monad parser-m
+ (m-when (> n 0)
+ (domonad [x item
+ xs (items (- n 1))]
+ (cons x xs))))) \ No newline at end of file