adding about map/filter/reduce
This commit is contained in:
parent
f3e063746a
commit
5aad06bf05
32
koans/about_higher_order_functions.clj
Normal file
32
koans/about_higher_order_functions.clj
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
(meditations
|
||||||
|
"The map function relates a sequence to another"
|
||||||
|
(= [__ __ __] (map (fn [x] (* 4 x)) [1 2 3]))
|
||||||
|
|
||||||
|
"You may create that mapping"
|
||||||
|
(= [1 4 9 16 25] (map (fn [x] __) [1 2 3 4 5]))
|
||||||
|
|
||||||
|
"Or use the names of existing functions"
|
||||||
|
(= __ (map nil? [:a :b nil :c :d]))
|
||||||
|
|
||||||
|
"A filter can be strong"
|
||||||
|
(= __ (filter (fn [x] false) '(:anything :goes :here)))
|
||||||
|
|
||||||
|
"Or very weak"
|
||||||
|
(= '(:anything :goes :here) (filter (fn [x] __) '(:anything :goes :here)))
|
||||||
|
|
||||||
|
"Or somewhere in between"
|
||||||
|
(= [10 20 30] (filter (fn [x] __) [10 20 30 40 50 60 70 80]))
|
||||||
|
|
||||||
|
"Maps and filters may be combined"
|
||||||
|
(= [10 20 30] (map (fn [x] __) (filter (fn [x] __) [1 2 3 4 5 6 7 8])))
|
||||||
|
|
||||||
|
"Reducing can increase the result"
|
||||||
|
(= __ (reduce (fn [a b] (* a b)) [1 2 3 4]))
|
||||||
|
|
||||||
|
"You can start somewhere else"
|
||||||
|
(= 2400 (reduce (fn [a b] (* a b)) __ [1 2 3 4]))
|
||||||
|
|
||||||
|
"Numbers are not the only things one can reduce"
|
||||||
|
(= "longest" (reduce (fn [a b]
|
||||||
|
(if (< __ __) b a))
|
||||||
|
["which" "word" "is" "longest"])))
|
@ -18,4 +18,5 @@
|
|||||||
(load "about_maps")
|
(load "about_maps")
|
||||||
(load "about_functions")
|
(load "about_functions")
|
||||||
(load "about_conditionals")
|
(load "about_conditionals")
|
||||||
|
(load "about_higher_order_functions")
|
||||||
(println "You have acheived clojure enlightenment. Namaste.")
|
(println "You have acheived clojure enlightenment. Namaste.")
|
||||||
|
Loading…
Reference in New Issue
Block a user