Last active
August 29, 2015 14:17
-
-
Save danshapero/0aa12ba21f8cacbe8d51 to your computer and use it in GitHub Desktop.
Some clojure macros for functional iteration
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
(defmacro iloop [[i istart iend] [sym init] expr] | |
`(loop [~i ~istart | |
~sym ~init] | |
(if (= ~i ~iend) | |
~sym | |
(recur (inc ~i) | |
~expr)))) | |
(defmacro iter [forms pred ret] | |
(let [syms (take-nth 3 forms) | |
inits (take-nth 3 (drop 1 forms)) | |
exprs (take-nth 3 (drop 2 forms))] | |
`(loop [~@(interleave syms inits)] | |
(if ~pred | |
~ret | |
(recur ~@exprs))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment