Bläddra i källkod

Final updates

Lily Carpenter 10 år sedan
förälder
incheckning
e2c4b3942b
1 ändrade filer med 207 tillägg och 16 borttagningar
  1. 207 16
      presentation.tex

+ 207 - 16
presentation.tex

@@ -39,6 +39,8 @@
39 39
   \item{Ratios}
40 40
   \item{Strings}
41 41
   \item{Regexes}
42
+  \item{Keywords}
43
+  \item{Symbols}
42 44
   \end{itemize}
43 45
 \end{frame}
44 46
 
@@ -46,7 +48,7 @@
46 48
   \frametitle{Integers}
47 49
   \begin{itemize}
48 50
   \item{Standard java Long}
49
-  \item{Automatically upgrade to arbitrary precision BigInteger}
51
+  \item{Automatically upgrade to arbitrary precision BigInteger with certain functions}
50 52
   \item{BigInteger does not exist for ClojureScript}
51 53
   \end{itemize}
52 54
 \end{frame}
@@ -56,12 +58,20 @@
56 58
   \begin{minted}{clojure}
57 59
     (+ 1 1)
58 60
     ;=> 2
59
-    (* 1 2)
60
-    ;=> 2
61 61
     (/ 6 3)
62 62
     ;=> 2
63
+    (> 3 2 1)
64
+    ;=> false
65
+    (< 3 2 1)
66
+    ;=> true
63 67
     (class (+ 1 1))
64 68
     ;=> java.lang.Long
69
+  \end{minted}
70
+\end{frame}
71
+
72
+\begin{frame}[fragile]
73
+  \frametitle{Integers}
74
+  \begin{minted}{clojure}
65 75
     ;; To the more experienced,
66 76
     ;; why does this not need +'?
67 77
     (class (+ 200000000000000000000
@@ -70,11 +80,12 @@
70 80
   \end{minted}
71 81
 \end{frame}
72 82
 
83
+
73 84
 \begin{frame}
74 85
   \frametitle{Floating points}
75 86
   \begin{itemize}
76 87
   \item{Standard java Double}
77
-  \item{Automatically upgrade to BigDecimal}
88
+  \item{Automatically upgrade to BigDecimal with certain functions}
78 89
   \item{BigDecimal does not exist for ClojureScript}
79 90
   \end{itemize}
80 91
 \end{frame}
@@ -140,6 +151,10 @@
140 151
     ;=> "BLAH"
141 152
     (.split "BLAH BLAH BLAH" "")
142 153
     ;=> ("BLAH" "BLAH" "BLAH")
154
+    (clojure.string/upper-case "blah")
155
+    ;=> "BLAH"
156
+    (clojure.string/split "BLAH BLAH BLAH" "")
157
+    ;=> ("BLAH" "BLAH" "BLAH")
143 158
   \end{minted}
144 159
 \end{frame}
145 160
 
@@ -162,6 +177,55 @@
162 177
   \end{minted}
163 178
 \end{frame}
164 179
 
180
+\begin{frame}
181
+  \frametitle{Keywords}
182
+  \begin{itemize}
183
+  \item{Identifiers that just evaluate to themselves}
184
+  \item{Can be used to lookup keys in maps}
185
+  \end{itemize}
186
+\end{frame}
187
+
188
+\begin{frame}[fragile]
189
+  \frametitle{Keywords}
190
+  \begin{minted}{clojure}
191
+    :foo
192
+    ;=> :foo
193
+    (keyword 'foo)
194
+    ;=> :foo
195
+    (:foo {:foo 1})
196
+    ;=> 1
197
+  \end{minted}
198
+\end{frame}
199
+
200
+\begin{frame}
201
+  \frametitle{Symbols}
202
+  \begin{itemize}
203
+  \item{"Variables": Not really, but partially serve the same purpose}
204
+  \item{They are identifiers pointing to something else}
205
+  \item{They don't vary (hence not really variables)}
206
+  \item{The actual storage location is called a var, symbol is just a name that points to it}
207
+  \end{itemize}
208
+\end{frame}
209
+
210
+\begin{frame}[fragile]
211
+  \frametitle{Symbols}
212
+  \begin{minted}{clojure}
213
+    (symbol 'foo)
214
+    ;=> foo
215
+    (symbol "foo")
216
+    ;=> foo
217
+    (symbol "clojure.core" "foo")
218
+    ;=> clojure.core/foo
219
+    (defn blah
220
+      []
221
+      (println "blah"))
222
+    ;=> #'user/blah
223
+    (let [x 5]
224
+      (+ x 2))
225
+    ;=> 7
226
+  \end{minted}
227
+\end{frame}
228
+
165 229
 \section{Sequences}
166 230
 \begin{frame}
167 231
   \frametitle{Sequences}
@@ -207,11 +271,10 @@
207 271
     ;=> 3
208 272
     (first '(1 2 3 4)) ; Using list as data structure
209 273
     ;=> 1
210
-    (let [list (conj '(1 2 3 4) 5)] ; Using as a stack
211
-      (peek list)
212
-      ;=> 5
213
-      (pop list)
214
-      ;=> (1 2 3 4))
274
+    (rest '(1 2 3 4))
275
+    ;=> (2 3 4)
276
+    (conj '(1 2 3 4) 5)
277
+    ;=> (5 1 2 3 4)
215 278
   \end{minted}
216 279
 \end{frame}
217 280
 
@@ -233,6 +296,8 @@
233 296
     ;=> [1 2 3 4 5]
234 297
     (conj [1 2 3 4] 5)
235 298
     ;=> [1 2 3 4 5]
299
+    (rseq [1 2 3 4 5])
300
+    ;=> (5 4 3 2 1)
236 301
   \end{minted}
237 302
 \end{frame}
238 303
 
@@ -250,12 +315,14 @@
250 315
   \begin{minted}{clojure}
251 316
     {:key1 1, :key1 2} ; Literal HashMap
252 317
     ;=> {:key1 2}
253
-    (hash-map :key1 1 :key1 2)
318
+    (hash-map :key1 1, :key1 2)
254 319
     ;=> {:key1 2}
255 320
     (assoc {} :key1 1)
256 321
     ;=> {:key1 1}
257 322
     (dissoc {:key1 1} :key1)
258 323
     ;=> {}
324
+    (conj {:key1 1} {:key2 2})
325
+    ;=> {:key1 1 :key2 2}
259 326
   \end{minted}
260 327
 \end{frame}
261 328
 
@@ -283,25 +350,149 @@
283 350
   \end{minted}
284 351
 \end{frame}
285 352
 
286
-\section{Sequence Functions}
287 353
 \begin{frame}
288
-  \frametitle{Sequence}
354
+  \frametitle{Basic collection functions}
289 355
   \begin{itemize}
290
-  \item{}
291
-  \item{}
356
+  \item{Reduce: Standard functional reduce(fold left), iterate over collection and return a single accumulated result.}
357
+  \item{Map: Standard functional map, iterate over collection and return a Sequence containing one value per a collection item.}
292 358
   \end{itemize}
293 359
 \end{frame}
294 360
 
295
-\section{Control Flow Functions}
361
+\begin{frame}[fragile]
362
+  \frametitle{Basic collection functions}
363
+  \begin{minted}{clojure}
364
+    (reduce + [1 2 3 4 5])
365
+    ;=> 15
366
+    (reduce conj #{} [:a :b :c])
367
+    ;=> #{:a :b :c}
368
+    (map inc [1 2 3])
369
+    ;=> (2 3 4)
370
+    (map + [1 2 3] [4 5 6] [7 8 9])
371
+    ;=> (12 15 18)
372
+    (defn wrong-arity-func
373
+      [a b]
374
+      (+ a b))
375
+    (map wrong-arity-func [1 2 3] [4 5 6] [7 8 9])
376
+    ;=> ArityException: Wrong number of args (3)
377
+    ;=> passed to wrong-arity-func
378
+  \end{minted}
379
+\end{frame}
380
+
381
+\section{Control Flow}
296 382
 \begin{frame}
297
-  \frametitle{Control Flow}
383
+  \frametitle{Looping/recursion}
384
+  \begin{itemize}
385
+  \item{Loop: Provides a lexical closure to recur to.}
386
+  \item{Recur: Used for loop control flow as well as for general recursive functions.}
387
+  \item{Recur will throw an exception if it is not in tail position}
388
+  \item{Quite often your use case can be solved using map/reduce or similar instead.}
389
+  \end{itemize}
390
+\end{frame}
391
+
392
+\begin{frame}[fragile]
393
+  \frametitle{Looping/recursion}
394
+  \begin{minted}{clojure}
395
+    (loop [x 10]
396
+      (when (> x 1)
397
+        (println x)
398
+        (recur (- x 2))))
399
+    ;=> 10
400
+    ;=> 2
401
+    (defn count-to-10 [x]
402
+      (when (<= x 10)
403
+        (println x)
404
+        (recur (inc x))))
405
+    (count-to-10 5)
406
+    ;=> 5
407
+    ;=> 10
408
+  \end{minted}
409
+\end{frame}
410
+
411
+\begin{frame}[fragile]
412
+  \frametitle{Looping/recursion}
413
+  \begin{minted}{clojure}
414
+    (defn count-to-10 [x]
415
+      (when (<= x 10)
416
+        (recur (inc x))
417
+        (println x)))
418
+    ;=> CompilerException java.lang
419
+    ;=> .UnsupportedOperationException: Can only
420
+    ;=>    recur from tail position
421
+  \end{minted}
422
+\end{frame}
423
+
424
+\begin{frame}
425
+  \frametitle{Conditionals}
426
+  \begin{itemize}
427
+  \item{If: Basic if statement, only support true and false branches for one statement. }
428
+  \item{Cond: More complex if, takes many different conditionals.}
429
+  \item{Case: Standard switch case statement}
430
+  \end{itemize}
431
+\end{frame}
432
+
433
+\begin{frame}[fragile]
434
+  \frametitle{Conditionals}
435
+  \begin{minted}{clojure}
436
+    (if (even? 6)
437
+      (println true)
438
+      (println false))
439
+    ;=> true
440
+    (if (even? 5)
441
+      (println true)
442
+      (println false))
443
+    ;=> false
444
+    (cond
445
+      (< 5 0) true
446
+      (> 5 0) false
447
+      :else nil)
448
+    ;=> false
449
+  \end{minted}
450
+\end{frame}
451
+
452
+\begin{frame}[fragile]
453
+  \frametitle{Conditionals}
454
+  \begin{minted}{clojure}
455
+    (cond
456
+      (< 0 5) true
457
+      (> 0 5) false
458
+      :else nil)
459
+    ;=> true
460
+    (let [mystr "no match"]
461
+      (case mystr
462
+        "" 0
463
+        "hello" (count mystr)
464
+        "default"))
465
+    ;=> "default"
466
+  \end{minted}
298 467
 \end{frame}
299 468
 
300 469
 \section{Macros}
301 470
 \begin{frame}
302 471
   \frametitle{Macros}
472
+  Very very very brief(and technically incorrect) explanation is: \\~\\
473
+
474
+    A 'function' that does not evaluate its arguments by default and returns
475
+    a list that will then be evaluated as a code form. \\~\\
476
+
477
+  Read \url{http://www.braveclojure.com/writing-macros/}
478
+\end{frame}
479
+
480
+\begin{frame}[fragile]
481
+  \frametitle{Macros}
482
+  \begin{minted}{clojure}
483
+    (defmacro pointless
484
+      [n]
485
+      n)
486
+    (pointless (+ 1 2))
487
+    ;=> 3
488
+    (pointless 3)
489
+    ;=> 3
490
+    (pointless [1 2 3 4])
491
+    ;=> [1 2 3 4]
492
+  \end{minted}
303 493
 \end{frame}
304 494
 
495
+
305 496
 \begin{frame}
306 497
   \begin{center}
307 498
     \Huge Questions?