->, ->> 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:

(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"}]}
view raw gistfile1.clj hosted with ❤ by GitHub

Comments

Popular posts from this blog

Regular expressions: Rust vs F# vs Scala

Hash maps: Rust, F#, D, Go, Scala

Haskell: performance