Skip to content
Next Next commit
removed force functions, using new method of lazy evaluation
  • Loading branch information
Jason Jackson committed Jun 3, 2011
commit 75795d6398bed8a45f66c6e3f00aab9f92004a9e
12 changes: 6 additions & 6 deletions src/eu/dnetlib/clojure/clarsec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@

(defmethod bind 'Parser
[dm dfunc]
(let [m (force dm)
func (force dfunc)]
(let [m dm
func dfunc]
(make-monad (monad-type m)
(fn [strn]
(let [parser (monad m)
result (parser strn)]
(if (consumed? result)
((force (monad (force (func (:value result))))) (:rest result))
((monad (func (:value result))) (:rest result))
result))))))

(defn result [v]
Expand All @@ -48,7 +48,7 @@
(make-monad 'Parser
(fn opt-plus [strn]
(failback
(first (drop-while failed? (map #((monad (force %)) strn) parsers)))
(first (drop-while failed? (map #((monad %) strn) parsers)))
(failed)))))

(defn >> [p1 p2]
Expand Down Expand Up @@ -104,7 +104,7 @@
(def many1)

(defn many [parser]
(>>== (optional (delay (many1 parser)))
(>>== (optional (many1 parser))
#(if (nil? %) () %)))

(defn many1 [parser]
Expand Down Expand Up @@ -195,7 +195,7 @@
(stringify (lexeme (between (is-char \") (is-char \") (many (not-char \"))))))

(defn parse [parser input]
((monad (force parser)) input))
((monad parser) input))

;;(defn -main []
;; (println (parse (>> (delay letter) (delay letter)) "ca.")))
2 changes: 1 addition & 1 deletion src/eu/dnetlib/clojure/monad.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#^{:doc "bind makes the value of the given monad available to a function.
The function may act on the value, but it must return another monad.
Although this cannot be enforced in Clojure."}
bind (fn [m _] (monad-type (force m))))
bind (fn [m _] (monad-type m)))

(defmethod bind `MZero [m _] m)
(defmethod bind `Monad [m f] (f (monad m)))
Expand Down