Skip to content

Commit

Permalink
awesome threading macro. needs clojure.walk to work properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlawrenceaspden committed Aug 1, 2013
1 parent 8b93817 commit f974158
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions threadingmacro.clj
@@ -0,0 +1,39 @@
;; robbed with thanks off of:
;; http://bryangilbert.com/code/2013/07/30/anatomy-of-a-clojure-macro/

(defn replace-if-underscore [element val]
(if (= element '_) val element))

(defn replace-underscores [form val]
(map #(replace-if-underscore % val) form))

(defn convert-forms [val [next-form & other-forms]]
(if (nil? next-form)
val
(let [next-val (gensym)]
`(let [~next-val ~(replace-underscores next-form val)]
~(convert-forms next-val other-forms)))))

(defmacro ->>> [init & forms]
(convert-forms init forms))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(->>> 20
(* _ _ _)
(* _ _ _)) ;-> 512000000000

(reduce * (repeat 9 20 )) ;-> 512000000000


(->>> #{}
(conj _ 'doom)
(conj _ 'horror)
(_ 'treason)) ;-> nil

(->>> #{}
(conj _ 'doom)
(conj _ 'horror)
(_ 'horror)) ;-> horror

;; Awesome. Votes for this in the standard library!

0 comments on commit f974158

Please sign in to comment.