Skip to content

Instantly share code, notes, and snippets.

@mecdemort
Created March 24, 2010 07:24
Show Gist options
  • Save mecdemort/342063 to your computer and use it in GitHub Desktop.
Save mecdemort/342063 to your computer and use it in GitHub Desktop.
(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