Skip to content

Instantly share code, notes, and snippets.

@danshapero
Last active August 29, 2015 14:17
Show Gist options
  • Save danshapero/0aa12ba21f8cacbe8d51 to your computer and use it in GitHub Desktop.
Save danshapero/0aa12ba21f8cacbe8d51 to your computer and use it in GitHub Desktop.
Some clojure macros for functional iteration
(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