Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns psykotic.threader
(:use clojure.walk))
(defmacro >> [& xs]
(reduce (fn [x1 x2]
(let [x1- (gensym)]
`(let [~x1- ~x1]
~(postwalk (fn [x] (if (= x '%) x1- x)) x2))))
(macroexpand-all xs)))
(println (>> 42 (+ 8 %) (/ % 2))) ; Vary threading position per expression
(println (>> 42 (+ 8 %) (* % %))) ; Refer multiple times to previous result, without reevaluation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment