2014-01-26 00:04:22 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "The map function relates a sequence to another"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= [4 8 12] (map (fn [x] (* 4 x)) [1 2 3]))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "You may create that mapping"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= [1 4 9 16 25] (map (fn [x] (* x x)) [1 2 3 4 5]))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Or use the names of existing functions"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= '(false false true false false) (map nil? [:a :b nil :c :d]))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "A filter can be strong"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= () (filter (fn [x] false) '(:anything :goes :here)))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Or very weak"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= '(:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here)))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Or somewhere in between"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= [10 20 30] (filter (fn [x] (<= x 30)) [10 20 30 40 50 60 70 80]))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Maps and filters may be combined"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= [10 20 30] (map (fn [x] (* x 10))
|
|
|
|
|
(filter (fn [x] (<= x 3)) [1 2 3 4 5 6 7 8])))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Reducing can increase the result"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= 24 (reduce (fn [a b] (* a b)) [1 2 3 4]))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "You can start somewhere else"
|
2016-02-29 23:25:51 +00:00
|
|
|
(= 2400 (reduce (fn [a b] (* a b)) (cons 100 [1 2 3 4])))
|
2010-02-11 04:13:55 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Numbers are not the only things one can reduce"
|
2010-02-11 04:13:55 +00:00
|
|
|
(= "longest" (reduce (fn [a b]
|
2016-02-29 23:25:51 +00:00
|
|
|
(if (< (count a)) b a))
|
2016-02-27 02:33:05 +00:00
|
|
|
["which" "word" "is" "longest"]))
|