-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch1.scratch.clj
366 lines (270 loc) · 7.57 KB
/
ch1.scratch.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
1
"String"
["a" "vector" "of" "strings"]
; (operator operand1 operand2 ... operandn)
(+ 1 2 3)
; => 6
(str "It was the panda " "in the library " "with a dust buster")
; => "It was the panda in the library with a dust buster"
; Control Flow
;(if boolean-form
; then-form
; optional-else-form)
(if true
"By Zeus's hammer!"
"By Aquaman's trident!")
; => "By Zeus's hammer!"
(if false
"By Zeus's hammer!"
"By Aquaman's trident!")
; => "By Aquaman's trident!"
;do
; The do operator lets you wrap up multiple forms in parentheses and run each of them.
(if true
(do (println "Success!")
"By Zeus's hammer!")
(do (println "Failure!")
"By Aquaman's trident!"))
; => Success!
; => "By Zeus's hammer!"
; The when operator is like a combination of if and do, but with no else branch. Here’s an example:
(when true
(println "Success!")
"abra cadabra")
; => Success!
; => "abra cadabra"
; Both nil and false are used to represent logical falsiness,
(nil? 1)
; => false
(nil? nil)
; => true
; Clojure’s equality operator is =:
(= 1 1)
; => true
(= nil nil)
; => true
(= 1 2)
; => false
(or false nil :large_I_mean_venti :why_cant_I_just_say_large)
; => :large_I_mean_venti
(or (= 0 1) (= "yes" "no"))
; => false
(or nil)
; => nil
(and :free_wifi :hot_coffee)
; => :hot_coffee
(and :feelin_super_cool nil false)
; => nil
;You use def to bind a name to a value in Clojure:
(def failed-protagonist-names
["Larry Potter" "Doreen the Explorer" "The Incredible Bulk"])
failed-protagonist-names
; => ["Larry Potter" "Doreen the Explorer" "The Incredible Bulk"]
(defn error-message
[severity]
(str "OH GOD! IT'S A DISASTER! WE'RE "
(if (= severity :mild)
"MILDLY INCONVENIENCED!"
"DOOOOOOOMED!")))
; Maps are similar to dictionaries or hashes in other languages.
; Empty map
{}
{:first-name "Charlie"
:last-name "McFishwich"}
;Here we associate "string-key" with the + function:
{"string-key" +}
; Maps can be nested
{:name {:first "John" :middle "Jacob" :last "Jingleheimerschmidt"}}
; Besides using map literals, you can use the hash-map function to create a map:
(hash-map :a 1 :b 2)
; => {:a 1 :b 2}
;You can look up values in maps with the get function:
(get {:a 0 :b 1} :b)
; => 1
(get {:a 0 :b {:c "ho hum"}} :b)
; => {:c "ho hum"}
(get {:a 0 :b 1} :c)
; => nil
(get {:a 0 :b 1} :c "unicorns?")
; => "unicorns?"
; The get-in function lets you look up values in nested maps:
(get-in {:a 0 :b {:c "ho hum"}} [:b :c])
; => "ho hum"
; Another way
({:name "The Human Coffeepot"} :name)
; => "The Human Coffeepot"
; Keywords
:a
:rumplestiltsken
:34
:_?
(:a {:a 1 :b 2 :c 3})
; => 1
;You can provide a default value, as with get:
(:d {:a 1 :b 2 :c 3} "No gnome knows homes like Noah knows")
; => "No gnome knows homes like Noah knows"
;Vectors
;A vector is similar to an array, in that it’s a 0-indexed collection. For example, here’s a vector literal:
[3 2 1]
(get [3 2 1] 0)
; => 3
(get ["a" {:name "Pugsley Winterbottom"} "c"] 1)
; => {:name "Pugsley Winterbottom"}
; You can create vectors with the vector function:
(vector "creepy" "full" "moon")
; => ["creepy" "full" "moon"]
; Lists are similar to vectors in that they’re linear collections of values. But there are some differences.
; For example, you can’t retrieve list elements with get.
'(1 2 3 4)
; => (1 2 3 4)
(list 1 "two" {3 4})
; => (1 "two" {3 4})
; Elements are added to the beginning of a list:
(conj '(1 2 3) 4)
; => (4 1 2 3)
; Sets
#{"kurt vonnegut" 20 :icicle}
(hash-set 1 1 2 2)
; => #{1 2}
(conj #{:a :b} :b)
; => #{:a :b}
; You can also create sets from existing vectors and lists by using the set function:
(set [3 3 3 4 4])
; => #{3 4}
(contains? #{:a :b} :a)
; => true
(contains? #{:a :b} 3)
; => false
(contains? #{nil} nil)
; => true
(:a #{:a :b})
; => :a
(get #{:a :b} :a)
; => :a
(get #{:a nil} nil)
; => nil
(get #{:a :b} "kurt vonnegut")
; => nil
;It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.
;—Alan Perlis
;Functions
;Calling Functions
(+ 1 2 3 4)
(* 1 2 3 4)
(first [1 2 3 4])
(or + -)
; => #<core$_PLUS_ clojure.core$_PLUS_@76dace31>
((or + -) 1 2 3)
; => 6
((and (= 1 1) +) 1 2 3)
; => 6
((first [+ 0]) 1 2 3)
; => 6
; these aren’t valid function calls, because numbers and strings aren’t functions:
(1 2 3 4)
("test" 1 2 3)
(inc 1.1)
; => 2.1
(map inc [0 1 2 3])
; => (1 2 3 4)
(defn no-params
[]
"I take no parameters!")
(defn one-param
[x]
(str "I take one parameter: " x))
(defn two-params
[x y]
(str "Two parameters! That's nothing! Pah! I will smoosh them "
"together to spite you! " x y))
(defn multi-arity
;; 3-arity arguments and body
([first-arg second-arg third-arg]
(do-things first-arg second-arg third-arg))
;; 2-arity arguments and body
([first-arg second-arg]
(do-things first-arg second-arg))
;; 1-arity arguments and body
([first-arg]
(do-things first-arg)))
(defn codger-communication
[whippersnapper]
(str "Get off my lawn, " whippersnapper "!!!"))
(defn codger
[& whippersnappers]
(map codger-communication whippersnappers))
(codger "Billy" "Anne-Marie" "The Incredible Bulk")
; => ("Get off my lawn, Billy!!!"
"Get off my lawn, Anne-Marie!!!"
"Get off my lawn, The Incredible Bulk!!!")
(defn favorite-things
[name & things]
(str "Hi, " name ", here are my favorite things: "
(clojure.string/join ", " things)))
(favorite-things "Doreen" "gum" "shoes" "kara-te")
; => "Hi, Doreen, here are my favorite things: gum, shoes, kara-te"
; Destructurin
;; Return the first element of a collection
(defn my-first
[[first-thing]] ; Notice that first-thing is within a vector
first-thing)
(my-first ["oven" "bike" "war-axe"])
; => "oven"
(defn chooser
[[first-choice second-choice & unimportant-choices]]
(println (str "Your first choice is: " first-choice))
(println (str "Your second choice is: " second-choice))
(println (str "We're ignoring the rest of your choices. "
"Here they are in case you need to cry over them: "
(clojure.string/join ", " unimportant-choices))))
(chooser ["Marmalade", "Handsome Jack", "Pigpen", "Aquaman"])
; => Your first choice is: Marmalade
; => Your second choice is: Handsome Jack
; => We're ignoring the rest of your choices. Here they are in case \
; you need to cry over them: Pigpen, Aquaman
(defn announce-treasure-location
[{lat :lat lng :lng}]
(println (str "Treasure lat: " lat))
(println (str "Treasure lng: " lng)))
(announce-treasure-location {:lat 28.22 :lng 81.33})
; => Treasure lat: 100
; => Treasure lng: 50
; Shorter
(defn announce-treasure-location
[{:keys [lat lng]}]
(println (str "Treasure lat: " lat))
(println (str "Treasure lng: " lng)))
; Keep access to the original map
(defn receive-treasure-location
[{:keys [lat lng] :as treasure-location}]
(println (str "Treasure lat: " lat))
(println (str "Treasure lng: " lng))
;; One would assume that this would put in new coordinates for your ship
(steer-ship! treasure-location))
(defn number-comment
[x]
(if (> x 6)
"Oh my gosh! What a big number!"
"That number's OK, I guess"))
(number-comment 5)
; => "That number's OK, I guess"
(number-comment 7)
; => "Oh my gosh! What a big number!"
;Anonymous Functions
(map (fn [name] (str "Hi, " name))
["Darth Vader" "Mr. Magoo"])
; => ("Hi, Darth Vader" "Hi, Mr. Magoo")
((fn [x] (* x 3)) 8)
; => 24
; Compact form
#(* % 3)
(#(str %1 " and " %2) "cornbread" "butter beans")
;Rest
(#(identity %&) 1 "blarg" :yip)
;Returning Functions
(defn inc-maker
"Create a custom incrementor"
[inc-by]
#(+ % inc-by))
(def inc3 (inc-maker 3))
(inc3 7)