Created
August 4, 2011 20:23
-
-
Save arohner/1126155 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;;; 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