-
Notifications
You must be signed in to change notification settings - Fork 0
/
Higher Order Functions
42 lines (22 loc) · 1.11 KB
/
Higher Order Functions
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
ClojureScript Koans Higher Order Functions Answers
http://clojurescriptkoans.com/#higher-order-functions/1
1)The map function relates a sequence to another
(= '( 4 8 12 )(map (fn [x] (* 4 x)) [1 2 3]))
2)You may create that mapping
(= '(1 4 9 16 25) (map (fn [x] (* x x) ) [1 2 3 4 5]))
3)Or use the names of existing functions
(= [false false true false false] (map nil? [:a :b nil :c :d]))
4)A filter can be strong
(= '() (filter (fn [x] false) '(:anything :goes :here)))
5)Or very weak
(= '(:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here)))
6)Or somewhere in between
(= [10 20 30] (filter (fn [x] (< x 40) ) [10 20 30 40 50 60 70 80]))
7)Maps and filters may be combined
(= [10 20 30] (map (fn [x] (* x 10) ) (filter (fn [x] (< x 4) ) [1 2 3 4 5 6 7 8])))
8)Reducing can increase the result
(= 24 (reduce (fn [a b] (* a b)) [1 2 3 4]))
9)You can start somewhere else
(= 2400 (reduce (fn [a b] (* a b)) 100 [1 2 3 4]))
10)Numbers are not the only things one can reduce
(= "longest" (reduce (fn [a b] (if (< (count a) (count b) ) b a)) ["which" "is" "the" "longest" "word"]))