-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1144 lines (965 loc) · 32.9 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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=1280" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Lemon Labs - Scala Beginner Workshop</title>
<meta name="description" content="Let's learn Scala" />
<meta name="author" content="Ian Forsey" />
<link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic" rel="stylesheet" type="text/css">
<link href="css/lemonlabs-slides.css" rel="stylesheet" />
<link href="css/idea.css" rel="stylesheet" />
<script type="text/javascript" src="js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body class="impress-not-supported">
<!--
Fallback message for clients that do not support impress.js
-->
<div class="fallback-message">
<p>Your browser <b>doesn't support the features required</b> by impress.js, so you are presented with a simplified version of this presentation.</p>
<p>For the best experience please use the latest <b>Chrome</b>, <b>Safari</b> or <b>Firefox</b> browser.</p>
</div>
<div id="impress">
<!--
Slides start here!
-->
<section class="step title" data-x="0" style="border-top-right-radius: 565px; border-top-left-radius: 565px; height: 719px; margin-top: 0px; padding-top: 125px;">
<img id="git-logo" src="images/git-logo-green-black.png" />
<h1 style="text-transform: uppercase;font-family: 'Montserrat', cursive;font-weight: 700;">
Scala Beginner Workshop
</h1>
<p class="subtle">Press <span class="key">➜</span> key</p>
<img id="logo" src="images/lemonlabs_logo_full.svg" />
<h3>
<a href="https://lemonlabs.io/git">
lemonlabs.io/git
</a>
</h3>
</section>
<div class="step" data-x="2000">
<h2>What is Scala?</h2>
<div class="step-content">
<ul>
<li>OO and Functional</li>
<ul>
<li>Everything is an object</li>
<li>Support closures</li>
</ul>
<li>Runs on the JVM</li>
<ul>
<li>Can call Java Libraries</li>
</ul>
<li>Statically typed</li>
</ul>
</div>
</div>
<div class="step" data-x="4000">
<h2>Why Scala?</h2>
<div class="step-content">
<ul>
<li>Scalable</li>
<ul>
<li>Codebase size</li>
<li>Multi core/node friendly</li>
</ul>
<li>Concise and expressive</li>
<li>Enjoyable to work with!</li>
</ul>
</div>
</div>
<div class="step" data-x="6000">
<h2>Agenda</h2>
<div class="step-content">
<ul>
<li><span class="keyword">val</span> and <span class="keyword">var</span></li>
<li><span class="keyword">def</span></li>
<li><span class="keyword">class</span> and <span class="keyword">case class</span></li>
<li>functions</li>
<li>collections and options</li>
<li><span class="keyword">trait</span></li>
<li><span class="keyword">object</span></li>
<li><span class="keyword">implicit</span></li>
</ul>
</div>
</div>
<section class="step title" data-x="8000">
<span class="keyword">val</span> and <span class="keyword">var</span>
</section>
<div class="step" data-x="10000">
<h2>vals and vars</h2>
<div class="step-content">
<ul>
<li><span class="keyword">val</span> (value)</li>
<ul>
<li>Once assigned cannot be reassigned</li>
</ul>
<li><span class="keyword">var</span> (variable)</li>
<ul>
<li>Can be reassigned</li>
</ul>
<li>Immutability first</li>
<ul>
<li>Use <span class="keyword">val</span> by default</li>
<li>Rarely need to use <span class="keyword">var</span></li>
</ul>
</ul>
</div>
</div>
<div class="step" data-x="12000">
<h2>Examples</h2>
<div class="step-content">
<pre><code class="scala">val title = "Jurassic Park"
val runtime = 12345
val is3d = false
val role = ("Sam Neil", "Dr. Alan Grant")
var rating = 0.8
rating = 0.9
</code></pre>
</div>
</div>
<div class="step" data-x="14000">
<h2>Task</h2>
<div class="step-content">
<ul>
<li>Assign <span class="string">"Jaws"</span> to a <span class="keyword">val</span> title</li>
<li>Assign <span class="number">402</span> to a <span class="keyword">var</span> gross</li>
<li>Assign a new value to title and gross</li>
</ul>
</div>
</div>
<div class="step" data-x="16000">
<h2>Examples</h2>
<div class="step-content">
<pre><code class="scala">val title<span class="codehl">: String</span> = "Jurassic Park"
val runtime<span class="codehl">: Int</span> = 12345
val is3d<span class="codehl">: Boolean</span> = false
val role<span class="codehl">: (String, String)</span> = ("Sam Neil", "Dr. Alan Grant")
var rating<span class="codehl">: Double</span> = 0.8
rating = 0.9</code></pre>
</div>
</div>
<section class="step title" data-x="18000">
<span class="keyword">def</span>
</section>
<div class="step" data-x="20000">
<h2>def (method)</h2>
<div class="step-content">
<pre><code class="scala">def movieProfit(cost: Int, gross: Int) =
gross - cost
movieProfit(100, 200)
</code></pre>
</div>
</div>
<div class="step" data-x="22000">
<h2>def (method)</h2>
<div class="step-content">
<pre><code class="scala">def movieProfit(cost: Int, gross: Int)<span class="codehl">: Boolean</span> =
gross - cost
movieProfit(100, 200)
</code></pre>
</div>
</div>
<div class="step" data-x="24000">
<h2>def (method)</h2>
<div class="step-content">
<pre><code class="scala">def movieProfit(cost: Int, gross: Int) = <span class="codehl">{</span>
val profit = gross - cost
println(s"Movie made $profit profit")
profit
<span class="codehl">}</span>
movieProfit(100, 200)
</code></pre>
</div>
</div>
<div class="step" data-x="26000">
<h2>Task</h2>
<div class="step-content">
<ul>
<li>Write a method called simpleRating</li>
<ul>
<li>it takes a <code>Double</code> argument called rating</li>
<li>returns <span class="string">"Excellent"</span> when rating is greater than <span class="number">8</span></li>
<li>returns <span class="string">"Okay"</span> when rating is between <span class="number">4</span> and <span class="number">8</span></li>
<li>otherwise returns <span class="string">"Poor"</span></li>
</ul>
<li>
<span class="keyword">if</span> expression syntax:
<pre><code class="scala">if ( <em>expr</em> ) <em>a</em> else if( <em>expr</em> ) <em>b</em> else <em>c</em></code></pre>
</li>
<li>
Equality check with <code>==</code>
</li>
</ul>
</div>
</div>
<div class="step" data-x="28000">
<h2>def (method)</h2>
<div class="step-content">
<pre><code class="scala">def simpleRating(rating: Double) =
if(rating > 8.0) "Excellent"
else if(rating > 4.0) "Okay"
else "Poor"
simpleRating(5.0)
</code></pre>
</div>
</div>
<div class="step" data-x="30000">
<h2>Default Parameters</h2>
<div class="step-content">
<pre><code class="scala">def numStars(rating: Double,
maxStars: Int <span class="codehl">= 10</span>): Int = {
require(rating >= 0.0 && rating <= 1.0)
math.round(rating * maxStars)
}
numStars(0.75)
numStars(0.75, 5)
</code></pre>
</div>
</div>
<div class="step" data-x="32000">
<h2>By Name Parameters</h2>
<div class="step-content">
<pre><code class="scala" style="font-size:36px; line-height:42px;">def numStars(rating: Double,
maxStars: Int = 10,
halfStars: Boolean = false): Double = {
require(rating >= 0.0 && rating <= 1.0)
if(halfStars)
math.round(rating * 2.0 * maxStars) / 2.0
else
math.round(rating * maxStars)
}
numStars(0.75, <span class="codehl">maxStars = 5</span>)
numStars(0.75, <span class="codehl">halfStars = true</span>)
numStars(<span class="codehl">rating = 0.75</span>)
</code></pre>
</div>
</div>
<section class="step title" data-x="34000">
<span class="keyword">class</span>
</section>
<div class="step" data-x="36000">
<h2>Classes</h2>
<div class="step-content">
<pre><code class="scala">
class Actor(firstName: String,
surname: String)
val tom = new Actor("Tom", "Hanks")
</code></pre>
</div>
</div>
<div class="step" data-x="38000">
<h2>Classes</h2>
<div class="step-content">
<pre><code class="scala">
class Actor(firstName: String,
surname: String) {
val name = firstName + " " + surname
}
val tom = new Actor("Tom", "Hanks")
tom.name
</code></pre>
</div>
</div>
<div class="step" data-x="40000">
<h2>Classes</h2>
<div class="step-content">
<pre><code class="scala">
class Actor(firstName: String,
surname: String) {
def name = firstName + " " + surname
}
val tom = new Actor("Tom", "Hanks")
tom.name
</code></pre>
</div>
</div>
<div class="step" data-x="42000">
<h2>Classes</h2>
<div class="step-content">
<pre><code class="scala">
class Actor(<span class="codehl">val</span> firstName: String,
<span class="codehl">val</span> surname: String)
val tom = new Actor("Tom", "Hanks")
tom.firstName
tom.surname
</code></pre>
</div>
</div>
<div class="step" data-x="44000">
<h2>Case Classes</h2>
<div class="step-content">
<pre><code class="scala">case class Actor(firstName: String,
surname: String)
</code></pre>
<ul class="nobullet">
<li class="plus-bullet">All fields made <span class="keyword">val</span>s (public)</li>
<li class="plus-bullet">Factory method (no <span class="keyword">new</span> keyword)</li>
<li class="plus-bullet"><code>==</code> and <code>hashcode</code> method</li>
<li class="plus-bullet">Pretty <code>toString</code> method</li>
<li class="neg-bullet">Case-to-case class inheritance is <a href="http://stackoverflow.com/questions/11158929/what-is-so-wrong-with-case-class-inheritance">prohibited</a></li>
</ul>
</div>
</div>
<div class="step" data-x="46000">
<h2>Case Class - Copy</h2>
<div class="step-content">
<pre><code class="scala">val tom = new Actor("Tom", "Hanks")
val tom2 = tom.copy(surname = "Cruise")
tom2.surname == "Cruise"</code></pre>
</div>
</div>
<div class="step" data-x="48000">
<h2>Case Class - Pattern Match</h2>
<div class="step-content">
<pre><code class="scala">val m = Movie(certificate = "U")
val ageRequirement = p match {
case Movie("U" | "PG") => 0
case Movie("12A") => 18
case Movie(cert) => cert.toInt
}</code></pre>
</div>
</div>
<div class="step" data-x="50000">
<h2>Task</h2>
<div class="step-content" style="font-size:30px; line-height:40px;">
<ul>
<li>Create a <span class="keyword">case class</span> called <code>Movie</code></li>
<ul>
<li>With a primary constructor containing</li>
<ul>
<li>an <code>Int</code> arg called <code>year</code></li>
<li>an <code>Int</code> arg called <code>gross</code></li>
<li>a <code>Double</code> arg called <code>rating</code></li>
</ul>
<li>and a method called <code>badge</code> that returns</li>
<ul>
<li><span class="string">"Golden Oldie"</span> if the <code>year</code> < <span class="number">1960</span></li>
<li><span class="string">"Blockbuster"</span> if the <code>gross</code> > <span class="number">20</span> and <code>rating</code> > <span class="number">7.0</span></li>
<li><span class="string">"Must see"</span> otherwise</li>
</ul>
</ul>
</ul>
<p style="text-align:center;">the AND operator is <code class="scala">&&</code></p>
</div>
</div>
<div class="step" data-x="52000">
<h2>Operators are methods</h2>
<div class="step-content">
<pre><code class="scala">case class Actor(name: String) {
def +(other: Actor) =
List(this, other)
}
Actor("Brad") + Actor("Angelina")</code></pre>
</div>
</div>
<section class="step" style="padding-top:100px" data-x="54000">
<img src="images/nodocs.jpg" />
<img src="images/tableflip.jpg" height="367" style="padding-left: 50px;"/>
</section>
<section class="step title" data-x="56000">
Functions
</section>
<div class="step" data-x="58000">
<h2>Functions</h2>
<div class="step-content">
<pre><code class="scala">
(<em>args</em>) => <em>body</em>
val funky = (<em>args</em>) => <em>body</em></code></pre>
</div>
</div>
<div class="step" data-x="60000">
<h2>Functions</h2>
<div class="step-content">
<pre><code class="scala">val success = (budget: Int, gross: Int) =>
budget < gross
val r = success(100, 200)
val r = success.apply(100, 200)</code></pre>
</div>
</div>
<div class="step" data-x="62000">
<h2>Task</h2>
<div class="step-content">
<pre><code class="scala">case class Movie(name: String, rating: Double)
val kb = Movie("Kill Bill", rating = 8.2)
val tr = Movie("The Room", rating = 3.5)
> greatFilm(kb) == true
> greatFilm(tr) == false</code></pre>
<br />
<p>Write the function <code>greatFilm</code>. It checks the <code>rating</code> is >= 7.0</p>
</div>
</div>
<div class="step" data-x="64000">
<h2>Task</h2>
<div class="step-content">
<pre><code style="font-size: 30px;" class="scala">val success =
(budget: <span class="codehl">Int</span>, gross: <span class="codehl">Int</span>) => budget < gross
val success: <span class="codehl">(Int, Int) => Boolean</span> =
(budget, gross) => budget < gross
val success: <span class="codehl">(Int, Int) => Boolean</span> =
_ < _</code></pre>
</div>
</div>
<section class="step title" data-x="66000">
Collections and Options
</section>
<div class="step" data-x="68000">
<h2>Collections</h2>
<div class="step-content">
<ul>
<li>List</li>
<ul><li>think Java LinkedList</li></ul>
<li>Vector</li>
<ul><li>think Java ArrayList</li></ul>
<li>Set</li>
<li>Map</li>
</ul>
</div>
</div>
<div class="step" data-x="70000">
<h2>Collections</h2>
<div class="step-content">
<pre><code class="scala">val movies = List(
Movie("Jurassic Park", 10.0f),
Movie("The Room", 3.5f),
Movie("Home Alone", 7.4f)
)
movies: List[Movie] = List(Movie(...</code></pre>
</div>
</div>
<div class="step" data-x="72000">
<h2>Collections</h2>
<div class="step-content">
<pre><code class="scala">val movies =
Movie("Jurassic Park", 10.0f) ::
Movie("The Room", 3.5f) ::
Movie("Home Alone", 7.4f) :: Nil
movies: List[Movie] = List(Movie(...</code></pre>
</div>
</div>
<div class="step" data-x="74000">
<h2>Collections</h2>
<div class="step-content">
<pre><code class="scala">movies.count(greatFilm)</code></pre>
<pre class="fadedcode">2
</pre>
<pre><code class="scala">movies.count(_.name.length > 10)</code></pre>
<pre class="fadedcode">1</pre>
</div>
</div>
<div class="step" data-x="76000">
<h2>Collections</h2>
<div class="step-content">
<pre><code class="scala">movies.filter(greatFilm)</code></pre>
<pre class="fadedcode">List(
Movie(Jurassic Park,10.0),
Movie(Home Alone,7.4))
</pre>
<pre><code class="scala">movies.filter(_.name.contains("J"))</code></pre>
<pre class="fadedcode">List(Movie(Jurassic Park,10.0))</pre>
</div>
</div>
<div class="step" data-x="78000">
<h2>Collections</h2>
<div class="step-content">
<pre><code class="scala">movies.map(greatFilm)</code></pre>
<pre class="fadedcode">List(true, false, true)
</pre>
<pre><code class="scala">movies.map(_.name)</code></pre>
<pre class="fadedcode">res: List[String] = List(
Jurassic Park,
The Room,
Home Alone)</pre>
</div>
</div>
<div class="step" data-x="80000">
<h2>Collections</h2>
<div class="step-content">
<pre><code style="font-size: 30px;" class="scala">movies.collect {
case Movie(name, rating) if rating < 4 => name
}</code></pre>
<pre class="fadedcode">res: List[String] = List(The Room)</pre>
</div>
</div>
<div class="step" data-x="82000">
<h2>Collections</h2>
<div class="step-content">
<pre><code style="font-size: 30px;" class="scala">val actor = List("Tom", "Val", "Anthony")
val chars = List("Maverick", "Iceman", "Goose")
actor zip chars</code></pre>
<pre class="fadedcode">res15: List[(String, String)] = List(
(Tom,Maverick),
(Val,Iceman),
(Anthony,Goose))</pre>
</div>
</div>
<section class="step" data-x="84000">
<img src="images/onelineofcode.jpg" style="height:550px;" />
</section>
<div class="step" data-x="86000">
<h2>Task</h2>
<div class="step-content">
<pre><code class="scala" style="font-size:30px; line-height:40px;">case class Movie(genre: String, gross: Int)
val movies = (100 to 5000 by 50).flatMap(g =>
Movie("Drama", g) :: Movie("Romcom", g) ::
Movie("Action", g) :: Nil)
</code></pre>
<ul>
<li>Work out the total gross of all the <span class="string">"Action"</span> <code class="scala">movies</code></li>
<li>Answer should be <span class="number">252450</span></li>
</ul>
<p style="text-align: center;"><br />You can call <code class="scala">.sum</code> on a <code class="scala">List[Int]</code> to get a total</p>
</div>
</div>
<div class="step" data-x="88000">
<h2>Some answers</h2>
<div class="step-content">
<pre><code class="scala">val res = movies.
filter(_.genre == "Action").
map(_.gross).
sum
val res = movies.collect {
case Movie("Action", gross) => gross
}.sum</code></pre>
</div>
</div>
<div class="step" data-x="90000">
<h2>Functions again</h2>
<div class="step-content">
<pre><code class="scala">movies.map <span class="yhl">(movie =>
if(movie.score < 4) "Flop"
else "Blockbuster"
)</span>
movies.collect <span class="yhl">{
case movie if movie.score < 4 => "Flop"
}</span></code></pre>
</div>
</div>
<div class="step" data-x="92000">
<h2>Functions again</h2>
<div class="step-content">
<pre><code class="scala">movies.map (movie =>
if(movie.score < 4) "Flop"
else "Blockbuster"
)
movies.collect {
case movie if movie.score < 4 => "Flop"
case _ => "Blockbuster"
}</code></pre>
</div>
</div>
<div class="step" data-x="94000">
<h2>Functions again</h2>
<div class="step-content">
<pre><code class="scala">movies.map (movie =>
if(movie.score < 4) "Flop"
else "Blockbuster"
)
movies.map {
case movie if movie.score < 4 => "Flop"
case _ => "Blockbuster"
}</code></pre>
</div>
</div>
<div class="step" data-x="96000">
<h2>Partial Functions</h2>
<div class="step-content">
<ul>
<li>Partial functions only take one argument (unary)</li>
<li>Doesn’t have to handle all possible values</li>
</ul>
<pre><code class="scala">val f: Int => String = {
case 0 => "zero!"
case 1 => "one!"
}</code></pre>
<p style="margin-top:20px; text-align:center; font-style: italic;">What happens if we call this with the argument 3?</p>
</div>
</div>
<div class="step" data-x="98000">
<h2>Options</h2>
<div class="step-content">
<ul>
<li>The <code class="scala">Option[T]</code> type is used to represent optional data</li>
<li>Two sub classes</li>
<ul>
<li><code class="scala">case class Some(val x)</code></li>
<li><code class="scala">case object None</code></li>
</ul>
</ul>
</div>
</div>
<section class="step" data-x="100000">
<img src="images/nullpointer.jpg" style="width:900px;"/>
</section>
<div class="step" data-x="102000">
<h2>Options</h2>
<div class="step-content">
<pre><code class="scala">val sort = Some("newest")
val sort = None
val sort = Option("newest")
val sort = Option(null)</code></pre>
</div>
</div>
<div class="step" data-x="104000">
<h2>Options</h2>
<div class="step-content">
<pre><code class="scala"> val sort: Option[String] = …
sort.getOrElse("newest")</code></pre>
</div>
</div>
<div class="step" data-x="106000">
<h2>Options</h2>
<div class="step-content">
<pre><code class="scala">val querySort: Option[String] = …
val headerSort: Option[String] = …
> val token = querySort.orElse(headerSort)
Some("newest")</code></pre>
</div>
</div>
<div class="step" data-x="108000">
<h2>Options</h2>
<div class="step-content">
<pre><code class="scala">val sort: Option[String] = …
val movies = sort
.map(sort => getMoviesFromDb(sort))
page = Some(List(Movie(...), Movie(...)))
page = None</code></pre>
</div>
</div>
<div class="step" data-x="110000">
<h2>Task</h2>
<div class="step-content">
<ul>
<li>Make a case class Rating</li>
<ul>
<li>It has a stars field of type Int</li>
<li>and a comment field of type Option[String]</li>
</ul>
<li>Give Rating a method asString that returns like so:</li>
</ul>
<pre><code class="scala">Rating(3, None).asString
"5* [no comment]"
Rating(1, Some("So awful")).asString
"1* [comment: So awful]"</code></pre>
</div>
</div>
<div class="step" data-x="112000">
<h2>Some Solutions</h2>
<div class="step-content">
<pre><code class="scala" style="font-size:34px; line-height:38px;">def asString =
stars + "* " +
comment.map("[comment:" + _ + "]").
getOrElse("[no comment]")
def asString =
stars + "* " + (
if(comment.nonEmpty) "[comment:" + comment.get + "]"
else "[no comment]")
def asString = stars + "* " + (comment match {
case Some(c) => s"[comment:$c]"
case None => "[no comment]"
})
</code></pre>
</div>
</div>
<div class="step" data-x="114000">
<h2>Callback Hell</h2>
<div class="step-content">
<pre><code class="scala">
val spielbergCast = genreOption.map { genre =>
genre.movies.map { movie =>
if(movie.director == "Spielberg")
movie.cast.map(_.name)
else List.empty
}
}
</code></pre>
</div>
</div>
<div class="step" data-x="116000">
<h2>For Expressions</h2>
<div class="step-content">
<pre><code class="scala">val spielbergCast = for {
genre <- genreOpt
movie <- genre.movies
if movie.director == "Spielberg"
actor <- movie.cast
} yield actor.name
</code></pre>
</div>
</div>
<section class="step title" data-x="118000">
<span class="keyword">trait</span>
</section>
<div class="step" data-x="120000">
<h2>Traits</h2>
<div class="step-content">
<ul>
<li>Like Classes but</li>
<ul>
<li>No constructor</li>
<li>Can do multiple inheritance</li>
</ul>
</ul>
</div>
</div>
<div class="step" data-x="122000">
<h2>Traits</h2>
<div class="step-content">
<pre><code class="scala">case class Award(name: String,
outcome: String)
// outcome is "Won" or "Nominated"
trait Person {
def awards: List[Award]
def awardsWon =
prods.filter(_.outcome == "Won")
}</code></pre>
</div>
</div>
<div class="step" data-x="124000">
<h2>Traits</h2>
<div class="step-content">
<pre><code class="scala">case class Actor(awards: List[Award])
extends Person
val seanBean = Actor(
Award("Best Lead Actor", "Won") :: Nil
)
seanBean.awardsWon.size == 1</code></pre>
</div>
</div>
<div class="step" data-x="126000">
<h2>Traits</h2>
<div class="step-content">
<pre><code class="scala" style="font-size:35px; line-height:45px;">case class Director(awards: List[Award])
val spielberg = new Director(
Award("Best Film", "Won") :: Nil
) with Person
spielberg.awardsWon.size == 0</code></pre>
</div>
</div>
<div class="step" data-x="128000">
<h2>Traits</h2>
<div class="step-content">
<ul>
<li>Define a trait called <code class="scala">Production</code> with and abstract def <code class="scala">releaseYear</code> of type <code class="scala">Int</code></li>
<li>Define a <code class="scala">Film</code> class; extends Production implement releaseYear with a year val</li>
<li>Bonus: Change <code class="scala">TvShow</code> to extends Production</li>
<ul><li>implement releaseYear with a method selecting the earliest series year</li></ul>
<pre><code class="scala" style="font-size:30px; line-height:40px;">
case class Series(year: Int)
case class TvShow(series: List[Series])
</code></pre>
</ul>
</div>
</div>
<div class="step" data-x="130000">
<h2>Traits</h2>
<div class="step-content">
<pre><code class="scala" style="font-size:28px; line-height:38px;">trait Production { def releaseYear: Int }
case class Film(releaseYear: Int) extends Production
case class TvShow(series: List[Series]) {
def releaseYear = series.sortBy(_.year).head.year
}
val thirtyRock = new TvShow(
Series(2011) :: Series(2012) :: Nil
) with Production
thirtyRock.releaseYear
res2: Int = 2011
</code></pre>
</div>
</div>
<section class="step title" data-x="132000">
<span class="keyword">object</span>
</section>
<div class="step" data-x="134000">
<h2>Objects</h2>
<div class="step-content">
<pre><code class="scala">object Counties {
val countries = "GB" :: "ZH" :: Nil
def supported(c: String) =
countries.contains(c)
}
> Countries.supported("ZH")
> true</code></pre>
</div>
</div>
<div class="step" data-x="136000">
<h2>Objects</h2>
<div class="step-content">
<pre><code class="scala">case class Movie(name: String)
object Movie {
def apply(name: String) =
new Movie(name)
}
> Movie("Jurassic Park")</code></pre>
</div>
</div>
<div class="step" data-x="138000">
<h2>Objects as enums</h2>
<div class="step-content">
<pre><code class="scala">sealed trait Outcome
case object Nominated extends Outcome
case object Won extends Outcome</code></pre>
</div>
</div>
<div class="step" data-x="140000">
<h2>Objects as enums</h2>
<div class="step-content">
<pre><code class="scala">
case class Award(outcome: Outcome)
val award = Award(Won)
val msg = award match {
case Award(Won) => "Woohoo!"
case Award(_) => "Unlucky"
}</code></pre>
</div>
</div>
<section class="step title" data-x="142000">
<span class="keyword">implicit</span>
</section>
<div class="step" data-x="144000">
<h2>Implicits</h2>
<div class="step-content">
<ul>
<li>The <code class="scala">implicit</code> keyword can be used:</li>
<ul>
<li>On a class/method - implicit conversions</li>
<li>On a <code class="scala">val</code> - implicit parameters</li>
</ul>
</ul>
</div>
</div>
<div class="step" data-x="146000">
<h2>Implicit conversions</h2>
<div class="step-content">
<pre><code class="scala">case class HttpRequest (
headers: List[HttpHeader]
)
case class HttpHeader (
name: String, value: String
)</code></pre>