-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreating Functions
More file actions
34 lines (17 loc) · 1.04 KB
/
Copy pathCreating Functions
File metadata and controls
34 lines (17 loc) · 1.04 KB
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
ClojureScript Koans Creating Functions Answers
http://clojurescriptkoans.com/#creating-functions/1
(defn square [x] (* x x))
1)One may know what they seek by knowing what they do not seek
(= [ true false true ] (let [not-a-symbol? (complement symbol?)] (map not-a-symbol? [:a 'b "c"])))
2)Praise and 'complement' may help you separate the wheat from the chaff
(= [:wheat "wheat" 'wheat] (let [not-nil? (complement nil?) ] (filter not-nil? [nil :wheat nil "wheat" nil 'wheat nil])))
3)Partial functions allow procrastination
(= 20 (let [multiply-by-5 (partial * 5)] (multiply-by-5 4)))
4)Don't forget: first things first
(= [:a :b :c :d] (let [ab-adder (partial concat [:a :b])] (ab-adder [:c :d])))
5)Functions can join forces as one 'composed' function
(= 25 (let [inc-and-square (comp square inc)] (inc-and-square 4 )))
6)Have a go on a double dec-er
(= 8 (let [double-dec (comp dec dec)] (double-dec 10)))
7)Be careful about the order in which you mix your functions
(= 99 (let [square-and-dec (comp dec square)] (square-and-dec 10)))