clojure-koans/09_higher_order_functions.clj

33 lines
1,009 B
Clojure
Raw Normal View History

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