-
Notifications
You must be signed in to change notification settings - Fork 4
/
lesson01.clj
237 lines (151 loc) · 3.85 KB
/
lesson01.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
;; We need this form, I'll explain why in a later class.
(ns clojure-training.lesson01)
;; This is a comment
;;;; This is also a comment. Typically to indicate a new section.
;; We'll talk about coding style and conventions at some point.
(comment
(this-is a commented out function call))
;;;; Syntax
;; Not:
;; 1 + 2; // but
(+ 1 2)
;; Lisp is verbose? Lots of stupid parentheses?
;; vs 1 + 2 + 3 + 4 + 5;
(+ 1
(+ 2
(+ 3
(+ 4 5))))
;; How about
(+ 1 2 3 4 5)
;; ( operator arg1 ... argN )
;; operators are almost, but not always, FUNCTIONS.
;;;; Fundamental types
"hello"
7
7.0
7/3 ; exact ratios
(* 7/3 3)
(* (/ 7.0 3.0) 3.0)
7N
java.lang.Long/MAX_VALUE
#_
(+ 9223372036854775807 1) ; ouch!
(+ 9223372036854775807N 1) ; OK!
;; The REPL
;; ---> Read Eval Print Loop
(comment ; a conceptual implementation
(loop
(print
(eval
(read)))))
:foo-bar ; I am a keyword
(comment
some-symbol) ; I am a symbol
{:liquid "Milk"
:colour "white"
:price 299}
[1 2 3] ; I am a vector
[1 :banana "split"]
;; Sets:
(= #{1 3 2}
#{3 1 2})
;; CONJ
(conj #{1 3 2} 99)
(into #{} (range 99))
(conj [1 2 3] 99)
(conj {:foo 99} [:blah 77])
(conj (list 1 2 3) 0)
#{1 2 3} ; I am a set order is immaterial
;;; VALUES.
;;; State: using DEF for VARS
(def foo 7)
(def grocery-entry {:liquid "Milk"
:colour "white"
:price 299})
;; ASSOC
(assoc grocery-entry
:on-special? true)
(dissoc grocery-entry :price)
(update grocery-entry :price inc)
(update grocery-entry :price - 100)
(defn put-on-special [entry discount]
;; mention that item is on special
;; ajust price to new discount
(dissoc
(update entry
(assoc entry :on-special? true)
:price * discount)
:liquid))
(defn put-on-special [entry discount]
;; mention that item is on special
;; ajust price to new discount
(-> entry
(assoc :on-special? true)
(update :price * discount)
(dissoc :liquid)))
(put-on-special grocery-entry 0.8)
;;;; FUNCTIONAL PROGRAMMING
+ ; These are 1st class values
;; Defining new functions: the FN operator
(fn [x] (* x x))
;; We can give them names:
(def square (fn [x] (* x x)))
;; This is so common there is a shortcut for this: DEFN
(defn square [x] (* x x)) ; same as above
;; Functional programming is about using functions as first class
;; objects in your program; that is both as arguments AND return
;; values to functions
(map square [1 2 3 4 5])
;; map is called a "higher order function", because one or more
;; of it's arguments are themselves functions.
;; Clojure is chock-full of "higher order functions"
;; can you name some?
(filter odd? [1 2 3 4 5])
;;; More higher order functions: function composition
;;; From basic algebra:
;; Given two functions f(x) and g(x), we can define
;; h(x) === f⎄g === f(g(x))
(defn f [x] (+ 1 x))
(defn g [x] (* 2 x))
(def h (comp f g))
(map g [1 2 3])
(def my-even (complement odd?))
(filter my-even [1 2 3 4 5]) ; behaves like even?
(filter even? [1 2 3 4 5])
(clojure.repl/doc filter)
;; practice:
;; - calling up doc strings of everything you type
;; - jumping to function definitions
;; - structural editing
;; Our fist special operator: or special form: LET
(def example
(let [x 7
y 3]
;;; ... in here
;; x and y exist.
(fn [in]
(+ x y in))))
(def add-3
(let [x 3]
; x is BOUND.
(fn [y] (+ x y))))
(add-3 10)
;; Generalize this:
(defn make-n-adder [n]
(let [x n]
(fn [y] (+ x y))))
(make-n-adder 99)
((make-n-adder 99) 10 )
;; remember this problem?
;; (letter-frequency "hello") => h 1 e 1 l 2 o 1
(first [1 2 3])
(rest [1 2 3])
(seq (rest []))
(if (rest [])
'hello
'nope)
;; Basic structure of the LOOP macro.
(loop [x [1 2 3]]
(println x)
(if (seq (rest x))
(recur (rest x))))