-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-hello-world.clj
84 lines (63 loc) · 1.53 KB
/
01-hello-world.clj
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
;; atom
1
;; function
(inc 1)
(dec 1)
;; call inc twice function
(inc (inc 1))
;; function composition
((comp inc inc) 1)
;; identity composition
((comp inc dec) 100)
;; identity function
(identity "hello, world")
;; multiple composititon
((comp inc inc inc inc) 0)
;; comp used alone is the identity
(comp 1)
;; prefix notation
(+ 1 2 3 4 5 6 7)
(* 2 3)
;; abstract algebra, sensible default adds to the identity
(+ 2)
;; additive identity
(+)
;; multiplicative identity
(*)
;; more tricky
(- 2 3 4)
;; 16 / 2 / 2 / 2
(/ 16 2 2 2)
;; order of precedence
(/ 2 (* 3 4))
;; Clojure is a hosted language, on top of the JVM. ClojureScript is a hosted language on top of Javascript
;; You are able to access language features from Java
Math/PI ;; returns 3.141...
(Math/cos Math/PI)
(Math/min 2 8)
;; predicate functions
(zero? 0)
(zero? (dec 2))
(pos? 4)
(neg? -19)
(< 0 1)
(< 0 1 2 3 4) ;; true
(< 0 1 1 3 4) ;; false
(<= 0 1 1 3 4) ;; true
(= 42 (inc 41) (dec 43))
;; type is important, it determines what you do with objects
;; types are not set in stone but rather decisions of the language
(number? 42) ;; true
(number? inc) ;; false
(fn? +) ;; true
(fn? inc) ;; true
(integer? 3) ;; true
(integer? 3.0) ;; false, ClojureScript would return true since it is based on Javascript's loose type system
(float? 3.0) ;; true
(float? 3) ;; false
;; symbolic fractions
(rational? (/ 1 2)) ;; true
(rational? 0.5) ;; false
;; symbolic fractions are different from floats
(= (/ 1 2) 0.5) ;; false
(== (/ 1 2) 0.5) ;; true