Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
awesome threading macro. needs clojure.walk to work properly.
- Loading branch information
1 parent
8b93817
commit f974158
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! |