Created
April 19, 2012 22:18
-
-
Save Cyrik/2424582 to your computer and use it in GitHub Desktop.
clojure recur stackoverflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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