finish chapter 9

This commit is contained in:
徐国方 2017-03-11 19:32:16 +08:00
parent 23d7980a12
commit 9030d4ac2f

View file

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