Skip to content

Instantly share code, notes, and snippets.

@MayDaniel
Created September 21, 2010 20:41
Show Gist options
  • Save MayDaniel/590490 to your computer and use it in GitHub Desktop.
Save MayDaniel/590490 to your computer and use it in GitHub Desktop.
(use 'clojure.walk)
(defmacro ->_
"Threads the forms, replacing underscores with the result of the last expression."
([x] x)
([x form] (if (seq? form)
(with-meta (postwalk-replace {'_ x} form) (meta form))
(list form x)))
([x form & more] `(->_ (->_ ~x ~form) ~@more)))
(comment
(macroexpand-all '(->_ 5 (+ 7 _) (* _ 9) (- _ 88)))
;; (- (* (+ 7 5) 9) 88)
(->_ [1 2 3] last (* _ _))
(->_ 5 (partial + _) (comp inc _ _) (_ 10))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment