Skip to content

Instantly share code, notes, and snippets.

@Cyrik
Created April 19, 2012 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cyrik/2424582 to your computer and use it in GitHub Desktop.
Save Cyrik/2424582 to your computer and use it in GitHub Desktop.
clojure recur stackoverflow
(defn atest [state]
(when-not (and (= "" state) (not (= (first state) \a)))
(list (first state) (. state (substring 1)))))
(defn op [state]
(when-not (and (= "" state) (not (= (first state) \a)))
(list #(list :| %1 %2) (. state (substring 1)))))
(defn chainl1-helper [x p op]
(fn [state]
(loop [x x
state state]
(if-let [xs (op state)]
(when-let [xs2 (p (second xs))]
(recur ((first xs) x (first xs2)) (second xs2)))
(list x state)))))
(defn chainl1 [p op]
(fn [state]
(when-let [[v s] (p state)]
((chainl1-helper v p op) s))))
(def test-parse (chainl1 atest op))
(defn stress-test [n] (test-parse (apply str (take n (interleave (repeat "a") (repeat "+"))))))
(stress-test 99999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment