Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created March 11, 2010 04:41
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 pervognsen/328830 to your computer and use it in GitHub Desktop.
Save pervognsen/328830 to your computer and use it in GitHub Desktop.
(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