Skip to content

Instantly share code, notes, and snippets.

@amalloy
Forked from arohner/gist:1126155
Created August 4, 2011 20:24
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 amalloy/1126157 to your computer and use it in GitHub Desktop.
Save amalloy/1126157 to your computer and use it in GitHub Desktop.
(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 init-val] (if (symbol? initial)
[initial initial]
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