summaryrefslogtreecommitdiffhomepage
path: root/clojure.html.markdown
diff options
context:
space:
mode:
authorLari Kovanen <lari@kovanen.se>2015-12-09 13:25:01 +0100
committerLari Kovanen <lari@kovanen.se>2015-12-09 13:25:01 +0100
commit46d3c28a5fc341f3b8ef061e963adfc7c610263e (patch)
tree794df6f192a3875dc09d2710395048c5f405a806 /clojure.html.markdown
parentdbfb19bb5779e84add18a19ebc36833e748e69d9 (diff)
parent1f76b2ad8c35b6c7e8ac2cc5dac8f20bc74f09ef (diff)
Merge remote-tracking branch 'adambard/master'
Diffstat (limited to 'clojure.html.markdown')
-rw-r--r--clojure.html.markdown25
1 files changed, 25 insertions, 0 deletions
diff --git a/clojure.html.markdown b/clojure.html.markdown
index a125d18f..58e835c9 100644
--- a/clojure.html.markdown
+++ b/clojure.html.markdown
@@ -264,6 +264,31 @@ keymap ; => {:a 1, :b 2, :c 3}
(print "Saying hello to " name)
(str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel")
+
+; Use the threading macros (-> and ->>) to express transformations of
+; data more clearly.
+
+; The "Thread-first" macro (->) inserts into each form the result of
+; the previous, as the first argument (second item)
+(->
+ {:a 1 :b 2}
+ (assoc :c 3) ;=> (assoc {:a 1 :b 2} :c 3)
+ (dissoc :b)) ;=> (dissoc (assoc {:a 1 :b 2} :c 3) :b)
+
+; This expression could be written as:
+; (dissoc (assoc {:a 1 :b 2} :c 3) :b)
+; and evaluates to {:a 1 :c 3}
+
+; The double arrow does the same thing, but inserts the result of
+; each line at the *end* of the form. This is useful for collection
+; operations in particular:
+(->>
+ (range 10)
+ (map inc) ;=> (map inc (range 10)
+ (filter odd?) ;=> (filter odd? (map inc (range 10))
+ (into [])) ;=> (into [] (filter odd? (map inc (range 10)))
+ ; Result: [1 3 5 7 9]
+
; Modules
;;;;;;;;;;;;;;;