Skip to content

Instantly share code, notes, and snippets.

@arohner
Created August 4, 2011 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arohner/1126155 to your computer and use it in GitHub Desktop.
Save arohner/1126155 to your computer and use it in GitHub Desktop.
;;; the docstring doesn't quite match the implementation, but you get the idea.
(defmacro fold
"sugar on reduce, similar to how 'for' is a nicer version of 'map'.
init is a symbol that evaluates to a value (local or var) or a vector of a symbol and an initial value. In the loop, init is bound to the result of the previous loop.
Binding can take any arguments supported by for, but should only bind one variable.
"
;; (fold r [i (range 10)] ;; existing symbol
;; (+ r i))
;; (fold [r 0] [i (range 10)] ;; define new local
;; (+ r i))
[initial binding & body]
(let [init-sym (if (symbol? initial)
initial
(first initial))
init-val (if (symbol? initial)
initial
(second initial))]
`(let [~init-sym ~init-val]
(reduce (fn [~init-sym ~(first binding)]
~@body) ~init-sym ~@(rest binding)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment