-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions
47 lines (24 loc) · 956 Bytes
/
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
43
44
45
46
47
ClojureScript Koans Functions Answers
http://clojurescriptkoans.com/#functions/1
(defn multiply-by-ten [n] (* 10 n))
(defn square [n] (* n n))
1)Calling a function is like giving it a hug with parentheses
(= 81 (square 9))
2)Functions are usually defined before they are used
(= 20 (multiply-by-ten 2))
3)But they can also be defined inline
(= 10 ((fn [n] (* 5 n)) 2))
4)Or using an even shorter syntax
(= 60 (#(* 15 %) 4))
5)Even anonymous functions may take multiple arguments
(= 15 (#(+ %1 %2 %3) 4 5 6))
6)Arguments can also be skipped
(= 30 (#(* 15 %2) 1 2))
7)One function can beget another
(= 9 (((fn [] (fn[x y](+ x y)) )) 4 5))
8)Functions can also take other functions as input
(= 20 ((fn [f] (f 4 5)) * ))
9)Higher-order functions take function arguments
(= 25 ( (fn [f] (f 5)) (fn [n] (* n n))))
10)But they are often better written using the names of functions
(= 25 ( (fn [f] (f 5)) square))