-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread-macros.clj
More file actions
46 lines (37 loc) · 1.11 KB
/
thread-macros.clj
File metadata and controls
46 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
;; Note that this operator (along with ->>) has at times been
;; referred to as a 'thrush' operator.
;; Perhaps easier to read:
(-> "a b c d"
.toUpperCase
(.replace "A" "X")
(.split " ")
first)
;"X"
(def person
{:name "Mark Volkmann"
:address {:street "644 Glen Summit"
:city "St. Charles"
:state "Missouri"
:zip 63304}
:employer {:name "Object Computing, Inc."
:address {:street "12140 Woodcrest Dr."
:city "Creve Coeur"
:state "Missouri"
:zip 63141}}})
(-> person :employer :address :city)
;"Creve Coeur"
;; An example of using the "thread-last" macro to get
;; the sum of the first 10 even squares.
(->> (range)
(map #(* % %))
(filter even?)
(take 10)
(reduce +))
;1140
;; This expands to:
(reduce +
(take 10
(filter even?
(map #(* % %)
(range)))))
;1140