->, ->> and thread-it
One guy suggested to use -> macro as often as possible instead of ->>. However, his final code doesn't look as clear as it should be (yes, it *should* be: we're coding in clojure).
I think, this is a great opportunity to use wonderful thread-it macro from "Clojure in Action" book:
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
(defmacro thread-it [& [first-expr & rest-exprs]] | |
(if (empty? rest-exprs) | |
first-expr | |
`(let [~'it ~first-expr] | |
(thread-it ~@rest-exprs)))) | |
(def jay {:name "jay fields" :employer "drw.com" :current-city "new york"}) | |
(def john {:name "john dydo" :employer "drw.com" :current-city "new york"}) | |
(def mike {:name "mike ward" :employer "drw.com" :current-city "chicago"}) | |
(def chris {:name "chris george" :employer "thoughtworks.com" :current-city "new york"}) | |
(thread-it | |
[jay john mike chris] | |
(filter (comp (partial = "new york") :current-city) it) | |
(group-by :employer it) | |
(update-in it ["drw.com"] (partial map :name))) | |
=> | |
{"drw.com" ("jay fields" "john dydo"), | |
"thoughtworks.com" | |
[{:name "chris george", | |
:current-city "new york", | |
:employer "thoughtworks.com"}]} |
Comments