-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·934 lines (838 loc) · 26.4 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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>reveal.js</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- 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 ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<section data-markdown>
<script type="text/template">

</script>
</section>
<section data-markdown>
<script type="text/template">
## An Introduction to Erlang
Wait, what?
- Erlang is (was) an obscure programming language invented in the 80s by Ericsson
<!-- .element: class="fragment" -->
- Built to support telephony switching equipment at a global scale
<!-- .element: class="fragment" -->
- Designed to be distributed, concurrent, highly-available, fault-tolerant, and hot-swappable
<!-- .element: class="fragment" -->
- Can achieve 9 nine's of availability (31.5569 milliseconds of downtime per year)
<!-- .element: class="fragment" -->
- Unfortunately, it is an ugly, ugly language
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
## An Introduction to Elixir
- A compiled, dynamically-typed, functional language that supports metaprogramming
<!-- .element: class="fragment" -->
- Built on top of Erlang and compiles to the same byte-code
<!-- .element: class="fragment" -->
- Can utilize Erlang code/libraries
<!-- .element: class="fragment" -->
- Has all benefits of Erlang...
<!-- .element: class="fragment" -->
- ...but with a Ruby-like syntax
<!-- .element: class="fragment" -->
Prediction: Likely to replace Ruby/Rails over the next few years.
<!-- .element: class="fragment" -->
</script>
</section>
</section>
<section>
<section data-markdown>
<script type-"text/template">
## Basic Types
Elixir has 8 basic types, all of which are immutable.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Integers
- Represent whole numbers
- Division of an Integer always produces a Float
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> 3 + 3
6
iex> 3 - 3
0
iex> 3 * 3
9
iex> 3 / 3
1.0
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Floats
- Represent decimal numbers
- Should not be trusted for important operations
<!-- .element: class="fragment" -->
- Third-party Decimal library should be used, may be promoted to standard library
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> 3.5 + 3.5
7.0
iex> 3.5 - 3.5
0.0
iex> 3.5 * 3.5
12.25
iex> 3.5 / 3.5
1.0
iex> 1.1 * 1.1
1.2100000000000002
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Booleans
- Represent logical propositions
- Comparisons do not support squishy concept of "truthiness"
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> true == true
true
iex> true == false
false
iex> true == 1
false
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Atoms
- Represent constant values
- Used primarily for programatic strings such as keys
<!-- .element: class="fragment" -->
- Boolean values are actually just Atoms
<!-- .element: class="fragment" -->
- Must follow variable naming unless quoted
<!-- .element: class="fragment" -->
- Should generally not be dynamically generated because of allocation limits in the language
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> :hello
:hello
iex> :true == true
true
iex> :1hello
** (SyntaxError) iex:17: unexpected token: ":"
iex> :"1hello"
:"1hello"
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Strings
- Are internally represented as UTF-8 binary byte lists
- Data length may not be the same as string length
<!-- .element: class="fragment" -->
- String module is UTF-8 aware
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> "hallå"
"hallå"
iex> String.to_charlist("hallå")
[104, 97, 108, 108, 229]
iex> IO.puts([104, 97, 108, 108, 229])
hallå
iex> String.length("hallå")
5
iex> Kernel.byte_size("hallå")
6
iex> String.upcase("hallå")
"HALLÅ"
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Lists
- Represent a set of values stored as a Linked List
- Item types can be mixed
<!-- .element: class="fragment" -->
- Can be concatenated and subtracted from each other
<!-- .element: class="fragment" -->
- Head/Tail operations on Lists are integral to the language for recursion
<!-- .element: class="fragment" -->
- Fast to prepend values, slow for everything else
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> Kernel.length([1, true, 2, false])
4
iex> ([1, true] ++ [2, false]) -- [true]
[1, 2, false]
iex> hd([1, true, 2, false])
1
iex> tl([1, true, 2, false])
[true, 2, false]
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Tuples
- Represent a set of values stored contiguously in memory
- Item types can be mixed
<!-- .element: class="fragment" -->
- Commonly used to return success/failure context from Functions
<!-- .element: class="fragment" -->
- Fast to read, slow to write
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> {:ok, 2}
{:ok, 2}
iex> Kernel.tuple_size({:ok, 2})
2
iex> Kernel.elem({:ok, 2}, 0)
:ok
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Functions
- Are first-class citizens which can be passed to, and returned from, other functions
- Are generally defined in Modules, but can be anonymous
<!-- .element: class="fragment" -->
- Are referenced by name + arity
<!-- .element: class="fragment" -->
- Final statement is implicit return, no explit returns
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> add = fn (n1, n2) -> n1 + n2 end
#Function<0.0000000/2 in :erl_eval.expr/5>
iex> add.(1, 2)
3
iex> Kernel.is_function(add)
true
iex> Kernel.is_function(add, 1)
false
iex> (&Kernel.is_function/2).(add, 2)
true
iex>
</code></pre>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type-"text/template">
## Associative Types
In addition to the basic types, Elixir has two important associative types
that are simply basic types of a specific shape.
</script>
</section>
<section data-markdown>
<script type-"text/template">
## Keyword Lists
- Are Lists of two-element Tuples containing an Atom key and arbitrary value
- Commonly used for optional function arguments
<!-- .element: class="fragment" -->
- Keys are ordered, must be Atoms, and may be duplicated
<!-- .element: class="fragment" -->
- Key values are taken from the front
<!-- .element: class="fragment" -->
- The Keyword module provides functions for advanced operations
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> list = [{:a, 1}, {:b, 2}, a: 3]
[a: 1, b: 2, a: 3]
iex> list[:a]
1
iex> Keyword.get_values(list, :a)
[1, 3]
</code></pre>
</script>
</section>
<section data-markdown>
<script type-"text/template">
## Maps
- Keys are not ordered, can be of any type, and are unique
- Commonly used to represent structured data
<!-- .element: class="fragment" -->
- Are the basis for named structs
<!-- .element: class="fragment" -->
- The Map module provides functions for advanced operations
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> %{hello: "world"}.hello
"world"
iex> %{100 => "One Hundred"}[100]
"One Hundred"
iex> Map.keys(%{a: 1, b: 2})
[:a, :b]
iex> map = %{a: 1, b: 2}
%{a: 1, b: 2}
iex> %{map | b: 3}
%{a: 1, b: 3}
</code></pre>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Operators
Elixir has all the normal operators you expect (+, -, /, *)
and a few that might look different...
</script>
</section>
<section data-markdown>
<script type="text/template">
## String Concatenation
<pre><code class="lang-elixir" data-trim>
iex> "Hello" <> " " <> "world"
"Hello world"
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Boolean Logic
- and, or, and not expect Boolean values
- &&, ||, and ! accept any value
<!-- .element: class="fragment" -->
- For the latter, anything that is not false or nil is "truthy"
<!-- .element: class="fragment" -->
<pre><code class="lang-elixir" data-trim>
iex> 1 and true
** (ArgumentError) argument error: 1
iex> 1 && true
true
iex> not nil
** (ArgumentError) argument error
:erlang.not(nil)
iex> !nil
true
iex> !"hello"
false
</code></pre>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Pattern Matching
One of the most important aspects of the language.
Elixir does not have assignment, it has pattern matching and capturing.
<!-- .element: class="fragment" -->
The = operator is simply an equality assertion that either:
<!-- .element: class="fragment" -->
- Raises an error if the assertion fails; or
<!-- .element: class="fragment" -->
- Returns the right-hand value if it succeeds.
<!-- .element: class="fragment" -->
If a variable is present in the left-hand value, the equivalent right-hand value will be copied to it unless pinned.
<!-- .element: class="fragment" -->
</script>
</section>
<section data-markdown>
<script type="text/template">
## Pattern Matching Assertions
Returns the right-hand value if the pattern match succeeds
<!-- .element: class="fragment" data-fragment-index="1" -->
<pre class="fragment" data-fragment-index="1"><code class="lang-elixir" data-trim>
iex> {100, "X"} = {100, "X"}
{100, "X"}
</code></pre>
Raises a MatchError if it does not
<!-- .element: class="fragment" data-fragment-index="2" -->
<pre class="fragment" data-fragment-index="2"><code class="lang-elixir" data-trim>
iex> {100, ""} = Integer.parse("100X")
** (MatchError) no match of right hand side value: {100, "X"}
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Pattern Matching Captures
- Values from the right-hand side are copied to variables on the left-hand side if the overall pattern is matched
<!-- .element: class="fragment" data-fragment-index="1" -->
- Multiple and nested assertions are supported
<!-- .element: class="fragment" data-fragment-index="1" -->
- Effectively HIGH-ENERGY destructuring assignments
<!-- .element: class="fragment" data-fragment-index="2" -->
<pre class="fragment" data-fragment-index="1"><code class="lang-elixir" data-trim>
iex> ret = {n, "X"} = Integer.parse("100X")
{100, "X"}
iex> ret
{100, "X"}
iex> n
100
</code></pre>
Variables can be forced to match based on their value rather than capture using the ^ (pin) operator
<!-- .element: class="fragment" data-fragment-index="3" -->
<pre class="fragment" data-fragment-index="3"><code class="lang-elixir" data-trim>
iex> {:ok, ^n} = {:ok, 0}
** (MatchError) no match of right hand side value: {:ok, 0}
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Pattern Matching Lists
<pre><code class="lang-elixir" data-trim>
iex> [{:a, a_value} | rest] = [a: 1, b: 2, c: 3]
[a: 1, b: 2, c: 3]
iex> a_value
1
iex> rest
[b: 2, c: 3]
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Pattern Matching Maps
<pre><code class="lang-elixir" data-trim>
iex> %{type: :error, msg: m} = %{type: :error, msg: "Failure"}
%{message: "Failure", type: :error}
iex> m
"Failure"
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Pattern Matching Insanity
<pre><code class="lang-elixir" data-trim>
iex> ret = {:error, %{reasons: ["One", "Two"], count: 2}}
{:error, %{count: 2, reasons: ["One", "Two"]}}
iex> {:error = status, %{
reasons: [first | _rest] = all,
count: c
}} = ret
{:error, %{count: 2, reasons: ["One", "Two"]}}
iex> status
:error
iex> first
"One"
iex> all
["One", "Two"]
iex> c
2
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## SURPRISE
All of this can be used in function definitions to match/capture against parameters.

</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Functions
Why we're all here, pay attention.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Declaration
Inline:
<!-- .element: class="fragment" data-fragment-index="1" -->
<pre class="fragment" data-fragment-index="1"><code class="lang-elixir" data-trim>
iex> hello = fn(who) -> "Hello" <> " " <> who end
#Function<0.00000000/1 in :erl_eval.expr/5>
iex> hello.("World")
"Hello World"
</code></pre>
Module:
<!-- .element: class="fragment" data-fragment-index="2" -->
<pre class="fragment" data-fragment-index="2"><code class="lang-elixir" data-trim>
defmodule Greeter do
def hello(who), do: build_hello(who)
defp build_hello(who), do: "Hello #{who}"
end
</code></pre>
<pre class="fragment" data-fragment-index="2"><code class="lang-elixir" data-trim>
iex> Greeter.hello("World")
"Hello World"
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Oneliners vs Multiliners
One-liners can be good for readability if the function is simple
<!-- .element: class="fragment" data-fragment-index="1" -->
<pre class="fragment" data-fragment-index="1"><code class="lang-elixir" data-trim>
defmodule MyModule do
def one_liner(arg), do: arg <> " one"
end
</code></pre>
Multi-liners require an end
<!-- .element: class="fragment" data-fragment-index="2" -->
<pre class="fragment" data-fragment-index="2"><code class="lang-elixir" data-trim>
defmodule MyModule
def multi_liner(arg) do
arg = arg <> " multi"
arg
end
end
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Piping
Functions can be piped using the `|>` symbol similar to unix pipes where the
output of one function is passed as the first parameter to the next function.
<pre class="fragment"><code class="lang-elixir" data-trim>
defmodule IntegerOperator do
def add(n1, n2), do: n1 + n2
def sub(n1, n2), do: n1 - n2
def mult(n1, n2), do: n1 * n2
end
</code></pre>
<pre class="fragment"><code class="lang-elixir" data-trim>
iex> import IntegerOperator
IntegerOperator
iex> 10 |> add(1) |> mult(3) |> sub(2) |> add(5)
36
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Pattern Matches
- Functions can pattern match on parameters
<!-- .element: class="fragment" -->
- Functions with different pattern matches can share names, first match wins
<!-- .element: class="fragment" -->
<pre class="fragment"><code class="lang-elixir" data-trim>
defmodule Greeter do
def greet("Hello:" <> v2), do: "Hello #{v2}"
def greet({:ok, v1, v2}), do: {v1, v2}
def greet(%{key1: %{sub_key: v1}, key2: v2}), do: %{v1 => v2}
def greet(_), do: "Hello...you"
end
</code></pre>
<pre class="fragment"><code class="lang-elixir" data-trim>
iex> Greeter.greet("Hello:World")
"Hello World"
iex> Greeter.greet({:ok, "Hej", "Världen"})
{"Hej", "Världen"}
iex> Greeter.greet(%{key1: %{sub_key: "Hola"}, key2: "Mundo"})
%{"Hola" => "Mundo"}
iex> Greeter.greet(true)
"Hello...you"
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Guards
- Functions can put constraints on parameters using a subset of language operators and functions
<!-- .element: class="fragment" -->
- This includes variables pulled from pattern matches
<!-- .element: class="fragment" -->
<pre class="fragment"><code class="lang-elixir" data-trim>
defmodule Math do
def div(n1, n2)
when is_integer(n1) and is_integer(n2) and n2 != 0
do
n1 / n2
end
end
</code></pre>
<pre class="fragment"><code class="lang-elixir" data-trim>
iex> Math.div(9, "3")
** (FunctionClauseError) no function clause matching in Math.div/2
iex> Math.div(9, 0)
** (FunctionClauseError) no function clause matching in Math.div/2
iex> Math.div(9, 3)
3.0
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Recursion
Using all these tools, common programming problems can be reimagined as combinations
of simple functions.
<pre class="fragment"><code class="lang-elixir" data-trim>
defmodule IntegerFinder do
def first_neg([]), do: nil
def first_neg([h | _t]) when is_integer(h) and h < 0, do: h
def first_neg([_h | t]), do: first_neg(t)
end
</code></pre>
<pre class="fragment"><code class="lang-elixir" data-trim>
iex> IntegerFinder.first_neg([])
nil
iex> IntegerFinder.first_neg(["hello", "world"])
nil
iex> IntegerFinder.first_neg([1, 2, 3])
nil
iex> IntegerFinder.first_neg([1, 2, -4, 3])
-4
</code></pre>
</script>
</section>
</section>
<!--
<section>
<section data-markdown>
<script type="text/template">
## Modules
Are used to:
- Organize code
- Contain functions
- Define exceptions
- Define named structs
- Define contracts for other modules
<pre><code class="lang-elixir" data-trim>
defmodule MyModule do
# Module body would be defined here.
end
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Exceptions
- Take the name of the module they are defined in
- Are defined within a module using `defexception`
<pre><code class="lang-elixir" data-trim>
defmodule MyError do
defexception message: "Error"
end
</code></pre>
<pre><code class="lang-elixir" data-trim>
iex> raise MyError
** (MyError) Error
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Structs
- Take the name of the module they are defined in
- Are defined within a module using `defstruct`
- When declared, define the keys they support along with defaults for each key
<pre><code class="lang-elixir" data-trim>
defmodule Animal do
defstruct [type: :human, name: ""]
end
</code></pre>
<pre><code class="lang-elixir" data-trim>
iex> %Animal{name: "Daniel"}
%Animal{name: "Daniel", type: :human}
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Behaviours
- Yes, it won't compile without the "u". ಠ_ಠ
- Define function signatures a module must implement to comply
<pre><code class="lang-elixir" data-trim>
defmodule Concatenator do
@callback concatenate(String.t, String.t) :: String.t
end
defmodule PipeConcatenator do
@behaviour Concatenator
def concatenate(s1, s2), do: s1 <> "|" <> s2
end
</code></pre>
<pre><code class="lang-elixir" data-trim>
iex> PipeConcatenator.concatenate("Hello", "World")
"Hello|World"
</code></pre>
</script>
</section>
</section>
-->
<section>
<section data-markdown>
<script type="text/template">
## Examples
Let's take a look at some common problems and how they could be solved the elixir way.
</script>
</section>
<section data-markdown data-transition="slide-in fade-out">
<script type="text/template">
## Projection (Javascript)
<pre><code class="lang-javascript" data-trim>
var IntegerProjector = {
addOne: function(numbers) {
var newNumbers = [];
for (var i = 0; i < numbers.length; i++) {
var n = numbers[i];
if (!(n === parseInt(n, 10))) {
throw new Error("Not an integer");
}
newNumbers.push(n + 1);
}
return newNumbers;
}
};
</code></pre>
</script>
</section>
<section data-markdown data-transition="fade-in slide-out">
<script type="text/template">
## Projection (Elixir)
<pre><code class="lang-elixir" data-trim>
defmodule IntegerProjector do
def add_one([]), do: []
def add_one([head | tail]) when is_integer(head) do
[head + 1] ++ add_one(tail)
end
end
</code></pre>
<pre class="fragment"><code class="lang-elixir" data-trim>
iex> IntegerProjector.add_one([])
[]
iex> IntegerProjector.add_one([1, "a"])
** (FunctionClauseError) no function clause matching in
IntegerProjector.add_one/1
iex:13: IntegerProjector.add_one(["a"])
iex:14: IntegerProjector.add_one/1
iex> IntegerProjector.add_one([1, 2, 3])
[2, 3, 4]
</code></pre>
</script>
</section>
<section data-markdown data-transition="slide-in fade-out">
<script type="text/template">
## Reduction (Javascript)
<pre><code class="lang-javascript" data-trim>
var IntegerReducer = {
sum: function(accumulator, numbers) {
for (var i = 0; i < numbers.length; i++) {
var n = numbers[i];
if (!(n === parseInt(n, 10))) {
throw new Error("Not an integer");
}
accumulator += n;
}
return accumulator;
}
};
</code></pre>
</script>
</section>
<section data-markdown data-transition="fade-in slide-out">
<script type="text/template">
## Reduction (Elixir)
<pre><code class="lang-elixir" data-trim>
defmodule IntegerReducer do
def sum(accumulator, []) when is_integer(accumulator) do
accumulator
end
def sum(accumulator, [head | tail])
when is_integer(accumulator) and is_integer(head)
do
sum((accumulator + head), tail)
end
end
</code></pre>
<pre class="fragment"><code class="lang-elixir" data-trim>
iex(16)> IntegerReducer.sum(10, [])
10
iex(17)> IntegerReducer.sum(10, ["a"])
** (FunctionClauseError) no function clause matching in
IntegerReducer.sum/2
iex:16: IntegerReducer.sum(10, ["a"])
iex(17)> IntegerReducer.sum(10, [1, 2, 3])
16
</code></pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## HTTP Response Handling
<pre><code class="lang-elixir" data-trim>
defmodule Getter
alias HTTPoison.Response as R
def get(url), do: url |> make_request |> handle_response
defp make_request(url), do: HTTPoison.get!(url)
defp handle_response(%R{status_code: code, headers: headers})
when code in [301, 302]
do
get(headers["Location"])
end
defp handle_response(%R{status_code: 404}) do
{:error, :not_found}
end
defp handle_response(%R{status_code: 200, body: body}) do
{:ok, body}
end
defp handle_response(_), do: {:error, :unhandled}
end
</code></pre>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Closing it out
There is so much more to Elixir
- Distributed execution of code across nodes
<!-- .element: class="fragment" -->
- Lightweight concurrency through the Language Process model
<!-- .element: class="fragment" -->
- In-place, hot-swappable code changes without system process restarts
<!-- .element: class="fragment" -->
- Fault-tolerant recovery of crashed Language Processes with no downtime
<!-- .element: class="fragment" -->
- Metaprogramming that allows for extending the language at compile-time
<!-- .element: class="fragment" -->
</script>
</section>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: '//cdn.socket.io/socket.io-1.3.5.js', async: true },
{ src: 'plugin/multiplex/master.js', async: true },
]
});
</script>
</body>
</html>