forked from toddthomas/elixir-basics-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
743 lines (624 loc) · 20.5 KB
/
index.html
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Elixir Basics Workshop</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="presentation/css/reveal.css">
<link rel="stylesheet" href="presentation/css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="presentation/lib/css/zenburn.css">
<style>
code { background: #3f3f3f; color: #dcdcdc;}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'presentation/css/print/pdf.css' : 'presentation/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2>Welcome To Boulder Elixir!</h2>
<p>To get set up:</p>
<pre><code>
#1 (optional)
# You can fork the repo first if you'd like, so it's your own copy
# url: github.com/boulder-elixir/elixir-basics-workshop
#2 Clone the repo
git clone
https://github.com/boulder-elixir/elixir-basics-workshop.git
#3 Open the project in your text editor
#4 Open your terminal and navigate to the project folder
</code></pre>
<p>Ask if you need help!</p>
</section>
<section>
<h2>Why Elixir?</h2>
<ul>
<li>Really fast</li>
<li>Easy to work with, even for big projects</li>
<li>Fantastic tooling</li>
<li>Concurrency!</li>
<li>Auto-recover failures</li>
<li>High up-time</li>
<li>Hot code reloading</li>
<li>Runs on BEAM (Erlang), 30 years battle tested</li>
<li><a href="https://www.youtube.com/watch?v=N4Duii6Yog0">Over 2 Million Concurrent Connections</a></li>
</ul>
</section>
<section>
<h2>Meetup Format & Timing</h2>
<ul>
<li>Presentations and occasionally workshops</li>
<li>Either last Tuesday or Thursday of every month</li>
</ul>
</section>
<section>
<h2>Workshop Goals</h2>
<ul>
<li>Learn basic syntax & how to work with Elixir</li>
<li>Be able to solve simple problems</li>
<li>Be exposed to Elixir's core features</li>
<li><strong>Be inspired to go home and learn more</strong></li>
</ul>
</section>
<section>
<h2>Starting IEX</h2>
<p>IEX is Elixir's interactive shell. Ships with Elixir.</p>
<p>In the console, run <code>iex</code>.</p>
<pre><code>
$ iex
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
</code></pre>
<p>To quit, press Ctrl + C twice.</p>
</section>
<section>
<h2>Assigning Variables</h2>
<pre><code>
iex> x = 2
2
iex> y = 3
3
# {...} are tuples
iex> {x, y} = {2, 3}
{2, 3}
iex> {a, b, c} = {4, 5, 6}
{4, 5, 6}
</code></pre>
<p>Mostly similar to other languages</p>
</section>
<section>
<h2>Pattern Matching</h2>
<pre><code>
iex> {:ok, val} = {:ok, 42}
{:ok, 42}
iex> val
42
iex> {:ok, val} = {:error, nil}
** (MatchError) no match of right hand side value: {:error, nil}
</code></pre>
<p>You don't actually assign variables, you pattern match them.</p>
<p>Variables get assigned if the pattern matches.</p>
<p>Equal sign works like how it does in math.</p>
</section>
<section>
<h2>Exercise 1</h2>
<ol>
<li>Load <code>iex</code></li>
<li>Assign <code>42</code> to a single variable</li>
<li>Assign values to the tuple <code>{:ok, value, message}</code> that pattern match</li>
<li>Try to assign values to the tuple <code>{:ok, value, message}</code> that DON'T pattern match</li>
</ol>
<p>-</p>
<p>Take 5 minutes. Josh and I will be walking around.</p>
<p>If you have a question, raise your hand or ask your neighbor.</p>
</section>
<section>
<h2>Exercise 1 Answer</h2>
<pre><code>
iex> value = 42
42
iex> {:ok, value, message} = {:ok, 5, "Hey there!"}
{:ok, 5, "Hey there!"}
iex> {:ok, value, message} = {:error, nil, "Nope!"}
** (MatchError) no match of right hand side value: {:error, nil, "Nope!"}
</code></pre>
</section>
<section>
<h2>If Statements</h2>
<pre><code>
iex> if true do
...> "Hello!"
...> else
...> "Goodbye!"
...> end
"Hello!"
</code></pre>
<p>Similar to what you're used to</p>
<p>Don't forget the <code>do</code>!</p>
</section>
<section>
<h2>Case Statements</h2>
<pre><code>
iex> case true do
...> true -> "Hello!"
...> _ -> "This will match anything except true"
...> end
"Hello!"
</code></pre>
<p>You will probably use <code>case</code> and pattern matching more than <code>if</code></p>
</section>
<section>
<h2>Case Statements With Pattern Matching</h2>
<pre><code>
iex> case {:ok, 42} do
...> {:ok, 42} -> "The number is 42!"
...> {:ok, val} -> "The number is not 42. It is: #{val}"
...> _ -> "Matches everything else."
...> end
"The number is 42!"
</code></pre>
</section>
<section>
<h2>Exercise 2</h2>
<ol>
<li>Write a <code>case</code> statement that handles the tuples <code>{:ok, 42}</code> and <code>{:error, nil}</code>.</li>
<li>Add a catch-all condition (underscore)</li>
</ol>
</section>
<section>
<h2>Exercise 2 Answer</h2>
<pre><code>
iex> case {:ok, 42} do
...> {:ok, value} -> value
...> {:error, _} -> "Error!"
...> _ -> "I'm not sure what to do..."
...> end
42
</code></pre>
</section>
<section>
<h2>Lists</h2>
<p>Known as Arrays in other languages.</p>
<p>Can contain multiple types.</p>
<p>Can concatenate with <code>++</code>.</p>
<pre><code>
iex> a = [1, 2, 3, 4]
[1, 2, 3, 4]
iex> b = [1, "a", true, 4]
[1, "a", true, 4]
iex> c = a ++ b
[1, 2, 3, 4, 1, "a", true, 4]
</code></pre>
</section>
<section>
<h2>The Enum Package</h2>
<p>Enum has lots of functions to manipulate Enumerables.</p>
<p>All of your favorite functions: map, reduce, sort, shuffle, etc.</p>
<pre><code>
iex> Enum.max([1, 3, 2])
3
iex> Enum.sort([3, 2, 4, 7, 1, 9])
[1, 2, 3, 4, 7, 9]
iex> Enum.shuffle([1, 2, 3, 4])
[4, 3, 1, 2]
</code></pre>
<p><a href="http://elixir-lang.org/docs/stable/elixir/Enum.html">Link to docs</a></p>
</section>
<section>
<h2>Enum cont.</h2>
<p>Many Enum functions take a function as an argument.</p>
<p>Map, for instance:</p>
<p><code>Enum.map(enumerable, function)</code></p>
<pre><code>
iex(13)> Enum.map([1, 2, 3], fn x -> x + 1 end)
[2, 3, 4]
</code></pre>
</section>
<section>
<h2>Detour: Anonymous Functions</h2>
<p>Use <code>fn (args) -></code> and <code>end</code> to define.</p>
<pre><code>
iex> add = fn (a, b) -> a + b end
#Function<12.90072148/2 in :erl_eval.expr/5>
iex> add.(1, 2)
3
</code></pre>
<p>Note you have to use <code>.</code> to invoke the function</p>
<p>Most often used when passing as an argument</p>
</section>
<section>
<h2>Let's see that Enum again</h2>
<pre><code>
iex(13)> Enum.map([1, 2, 3], fn x -> x + 1 end)
[2, 3, 4]
</code></pre>
<p>Passing in an anonymous function that Enum calls</p>
</section>
<section>
<h2>Pipe Character</h2>
<p>Similar to Unix pipe</p>
<p>Takes output of first function and passes as first argument to next function</p>
<pre><code>
iex(13)> Enum.map([1, 2, 3], fn x -> x + 1 end) |> Enum.take(1)
[2]
# equivalent to
list = Enum.map([1, 2, 3], fn x -> x + 1 end)
Enum.take(list, 1)
</code></pre>
</section>
<section>
<h2>Exercise 3</h2>
<ol>
<li>Combine two lists</li>
<li>Use <code>Enum.map</code> to square this list of numbers: <code>[1, 2, 3, 4]</code></li>
<li>Using the code from #2 and the pipe character, square a list of numbers and then shuffle them using <code>Enum.shuffle()</code></li>
</ol>
</section>
<section>
<h2>Exercise 3 Answer</h2>
<pre><code>
iex> [1, 2, 3] ++ [3, 4, 5]
[1, 2, 3, 3, 4, 5]
iex> Enum.map([1, 2, 3, 4], fn x -> x * x end)
[1, 4, 9, 16]
iex> Enum.map([1, 2, 3, 4], fn x -> x * x end) |> Enum.shuffle
[4, 16, 1, 9]
</code></pre>
</section>
<section>
<h2>Maps</h2>
<p><code>%{key: value, key: value}</code></p>
<pre><code>
iex> a = %{a: 13, b: true, c: "cat"}
%{a: 13, b: true, c: "cat"}
iex> a.a
13
iex> a.c
"cat"
iex> a[:c]
"cat"
iex> Map.put(a, :d, "hey there") # Map.put(map, key, value)
%{a: 13, b: true, c: "cat", d: "hey there"}
</code></pre>
</section>
<section>
<h2>Enum With Maps</h2>
<p>Pass in the map and a function</p>
<pre><code>
iex> my_map = %{a: 1, b: 2, c: 3}
%{a: 1, b: 2, c: 3}
iex> Enum.map(my_map, fn {key, val} -> val * val end)
[1, 4, 9]
iex> Enum.map(my_map, fn {key, val} -> val * val end) |> Enum.max
9
iex> Enum.filter(my_map, fn {key, val} -> val == 2 end)
[b: 2]
</code></pre>
</section>
<section>
<h2>Exercise 4</h2>
<ol>
<li>Given the map <code>%{a: 1, b: 2}</code>, add the element <code>c: 3</code>. Hint: Use <code>Map.put(enum, key, value)</code></li>
<li>Use <code>Enum.filter(enum, function)</code> to get all items in <code>%{a: 3, b: 12, c: 9, d: 15}</code> where value is greater than 10. </li>
<li>Using pipes and <code>Enum.take(enum, number)</code>, modify code in #1 to only return 1 item.</li>
</ol>
<p>Hint: Use <code>{key, value}</code> to extract the keys and values in your anonymous function</p>
</section>
<section>
<h2>Exercise 4 Answer</h2>
<pre><code>
iex> map = %{a: 3, b: 12, c: 9, d: 15}
%{a: 3, b: 12, c: 9, d: 15}
iex> Enum.filter(map, fn {key, val} -> val > 10 end)
[b: 12, d: 15]
iex> Enum.filter(map, fn {key, val} -> val > 10 end) |> Enum.take(1)
[b: 12]
</code></pre>
</section>
<section>
<h2>Modules</h2>
<p>Let's put our code into files!</p>
<pre><code>
# lib/workshop.exs
defmodule Workshop do
def add(a, b) do
a + b
end
end
</code></pre>
</section>
<section>
<h2>Running Modules In iex</h2>
<p>You have to tell iex to compile the file</p>
<p>Compiles into Erlang bytecode</p>
<pre><code>
$ iex lib/workshop.exs
...
iex> Workshop.add(1, 2)
3
# can also compile from within iex
iex> c "lib/workshop.exs"
[Workshop]
</code></pre>
</section>
<section>
<h2>Private Functions</h2>
<p>Can only be called from its module</p>
<p>Cannot be unit tested</p>
<pre><code>
# lib/workshop.exs
defmodule Workshop do
...
defp log_value(val) do
IO.puts "Value is: #{val}"
end
end
</code></pre>
</section>
<section>
<h2>Exercise 5</h2>
<ol>
<li>Add a <code>sum</code> function that sums every number in a list</li>
<li>Uncomment the tests for <code>sum</code> in <code>test/workshop_test.exs</code> and make them pass</li>
</ol>
<p>Hint: use <code>Enum.reduce(enum, 0, fn (num, accumulator))</code></p>
<p>Run tests with <code>mix test</code> in console.</p>
</section>
<section>
<h2>Exercise 5 Answer</h2>
<pre><code>
def sum(list) do
Enum.reduce(list, 0, fn (num, acc) -> num + acc end)
end
</code></pre>
</section>
<section>
<h3>Pattern Matching With Functions</h3>
<p>Function signatures can use pattern matching too!</p>
<pre><code>
# lib/workshop.exs
defmodule Workshop do
...
def divide(_, 0) do
"You can't divide by zero!"
end
def divide(dividend, divisor) do
dividend / divisor
end
end
</code></pre>
<p>Multiple functions will get called based on what matches</p>
</section>
<section>
<h3>Function Guards</h3>
<p>You can further restrict if a function is called with a guard clause</p>
<pre><code>
# lib/workshop.exs
defmodule Workshop do
...
def guarded_divide(dividend, divisor) when divisor > 0 do
dividend / divisor
end
end
</code></pre>
</section>
<section>
<h2>Exercise 6</h2>
<p>Let's write FizzBuzz!</p>
<ol>
<li>Write multiple versions of the function <code>fizzbuzz</code></li>
<li>If called with a variable of <code>3</code>, return <code>"Fizz"</code></li>
<li>If called with a variable of <code>5</code>, return <code>"Buzz"</code></li>
<li>If called with a variable of <code>3</code> and <code>5</code>, return <code>"FizzBuzz"</code></li>
<li>If called with anything else, simply return what was passed in</li>
</ol>
<p>Uncomment and run tests with <code>mix test</code>.</p>
<p>Hint: To check a remainder use <code>rem(number, 3)</code></p>
<p>Hint: The first function defined is the first to match</p>
</section>
<section>
<h2>Exercise 6 Answer</h2>
<pre><code>
def fizzbuzz(n) when rem(n, 3) == 0 and rem(n, 5) == 0 do
"FizzBuzz"
end
def fizzbuzz(n) when rem(n, 3) == 0 do
"Fizz"
end
def fizzbuzz(n) when rem(n, 5) == 0 do
"Buzz"
end
def fizzbuzz(n) do
n
end
</code></pre>
</section>
<section>
<h3>Recursion</h3>
<p>Basic definition: when a function calls itself</p>
<p>Because Elixir is a functional language, recursion is used in place of loops</p>
<p>Functions in Enum are implemented using recursion</p>
</section>
<section>
<h3>Lists</h3>
<p>All lists are linked lists</p>
<p>Every list can be separated into the first element and the rest of the list</p>
<pre><code>
iex> [head | tail] = [1, 2, 3, 4]
[1, 2, 3, 4]
iex> head
1
iex> tail
[2, 3, 4]
</code></pre>
</section>
<section>
<h3>Lists</h3>
<p>Last element of every list is an empty list</p>
<pre><code>
iex> [head | tail] = [1]
[1]
iex> head
1
iex> tail
[]
</code></pre>
</section>
<section>
<h3>Recursion With Lists</h3>
<p>We can define recursive functions using pattern matching and using multiple functions</p>
<pre><code>
def thing([]) do
[]
end
def thing([head | tail]) do
...
end
</code></pre>
<p>Because last element is always empty list, that is our base case</p>
<p>After that, we can define functions that do the work</p>
</section>
<section>
<h3>Recursion With Lists - Example</h3>
<p>Let's add 1 to every number in a list</p>
<pre><code>
def add_one([]) do
[]
end
</code></pre>
<p>Define base case</p>
</section>
<section>
<h3>Recursion With Lists - Example</h3>
<p>Let's add 1 to every number in a list</p>
<pre><code>
def add_one([]) do
[]
end
def add_one([head | tail]) do
# todo
end
</code></pre>
<p>Define second function and assign first element <code>head</code> and rest of list <code>tail</code></p>
</section>
<section>
<h3>Recursion With Lists - Example</h3>
<p>Let's add 1 to every number in a list</p>
<pre><code>
def add_one([]) do
[]
end
def add_one([head | tail]) do
[head + 1 | ...]
end
</code></pre>
<p>In second function, declare new list with <code>head + 1</code> as the first element</li>
</section>
<section>
<h3>Recursion With Lists - Example</h3>
<p>Let's add 1 to every number in a list</p>
<pre><code>
def add_one([]) do
[]
end
def add_one([head | tail]) do
[head + 1 | add_one(tail)]
end
</code></pre>
<p>Recursively fill in the rest of the elements (call <code>add_one</code>)</p>
<p>When no more elements, the first function matches, and the stack returns</p>
</section>
<section>
<h2>In iex</h2>
<pre><code>
iex> Workshop.add_one([])
[]
iex> Workshop.add_one([1])
[2]
iex> Workshop.add_one([1, 2, 3])
[2, 3, 4]
iex> Workshop.add_one([1, 2, 3, 10])
[2, 3, 4, 11]
</code></pre>
</section>
<section>
<h2>Exercise 7</h2>
<p>Using recursion and pattern matching, write the function <code>square_list</code> which squares a list of numbers</p>
<ol>
<li>Define your base case (handle empty list)</li>
<li>Define second function that assigns <code>head</code> and <code>tail</code></li>
<li>Return a new list with the first element being the squared <code>head</code></li>
<li>Recursively fill in the rest of the list by calling <code>square_list</code></li>
</ol>
<p>Uncomment and run tests with <code>mix test</code>.</p>
</section>
<section>
<h2>Exercise 7 Answer</h2>
<pre><code>
def square_list([]) do
[]
end
def square_list([head | tail]) do
[head * head | square_list(tail)]
end
</code></pre>
</section>
<section>
<h2>Discussion</h2>
<p>Too hard?</p>
<p>Too easy?</p>
<p>Other thoughts?</p>
</section>
<section>
<h2>Group Sites & Chat</h2>
<p>GitHub: <a href="https://github.com/boulder-elixir">https://github.com/boulder-elixir</a></p>
<p>Elixir Slack Group, there's a #boulder channel</p>
<p>Feel free to message me at <code>tracehelms</code></p>
<p>Sign Up: <a href="https://elixir-slackin.herokuapp.com/">https://elixir-slackin.herokuapp.com/</a></p>
</section>
<section>
<h2>Next Time</h2>
<p>Let me know if you want to give a presentation!</p>
</section>
</div>
</div>
<script src="presentation/lib/js/head.min.js"></script>
<script src="presentation/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'presentation/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'presentation/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'presentation/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'presentation/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'presentation/plugin/zoom-js/zoom.js', async: true },
{ src: 'presentation/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>