Created
March 24, 2010 07:24
-
-
Save mecdemort/342063 to your computer and use it in GitHub Desktop.
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
(use 'clojure.walk) | |
;from psykotic http://gist.github.com/328830 | |
(defmacro >> [& xs] | |
(reduce (fn [x1 x2] | |
(let [x1- (gensym)] | |
`(let [~x1- ~x1] | |
~(postwalk (fn [x] (if (= x '%) x1- x)) x2)))) | |
(macroexpand-all xs))) | |
;allows forms that are not lists (>> 5 (+ 1 %) print) -> (print (+ 1 5)) | |
(defmacro >> [& xs] | |
(reduce (fn [x1 x2] | |
(if (seq? x2) | |
(let [x1- (gensym)] | |
`(let [~x1- ~x1] | |
~(postwalk (fn [x] (if (= x '%) x1- x)) x2))) | |
(list x2 x1))) | |
(macroexpand-all xs))) | |
;nilsafe >> | |
(defmacro >?> [& xs] | |
(reduce (fn [x1 x2] | |
(cond | |
(nil? x1) nil | |
(seq? x2) | |
(let [x1- (gensym)] | |
`(let [~x1- ~x1] | |
~(postwalk (fn [x] (if (= x '%) x1- x)) x2))) | |
:else (list x2 x1))) | |
(macroexpand-all xs))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment