-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
807 lines (692 loc) · 27.4 KB
/
index.html
File metadata and controls
807 lines (692 loc) · 27.4 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Scala is not Java</title>
<meta name="description" content="Some arguments that Scala code doen not need to look like Java without semicolons" />
<meta name="author" content="Mateusz Kubuszok" />
<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="css/reveal.css" />
<link rel="stylesheet" href="css/theme/scalac.css" id="theme" />
<link rel="stylesheet" href="lib/css/darkula.css" />
<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>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Scala is not Java</h1>
<h3><a href="http://kubuszok.pl">Mateusz Kubuszok</a> <b>></b> <a href="https://scalac.io">Scalac</a></h3>
</section>
<section>
<h2>Agenda</h2>
<ul>
<li class="fragment">Scala's features</li>
<li class="fragment">some FP "design patterns"</li>
<li class="fragment">some OOP design patterns</li>
<li class="fragment">(not) porting Java approach to Scala</li>
</ul>
</section>
<section>
<section>
<h1>Scala's features</h1>
</section>
<section>
<h2>var, val and type inference</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">void publish(
Message message,
Publisher publisher) {
Timestamp timestamp = makeTimestamp();
publisher.send(message, timestamp);
}</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">def publish(
message: Message
publisher: Publisher): Unit = {
val timestamp = makeTimestamp()
publisher.send(message, timestamp)
}</code></pre></td>
</tr>
</table>
<aside class="notes">Mention Michael Feathers's Working with a Legacy Code and how we used final to find bugs in code.</aside>
</section>
<section>
<h2>functions</h2>
<pre class="fragment"><code class="hljs scala">def myFun1(int: Int): String = int.toString
val myFun2: String => String = _.toUpperCase
val myFun3 = myFun1 andThen myFun2
List(1, 2, 3).map(myFun3)</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>pattern matching</h2>
<pre class="fragment"><code class="hljs scala">result match {
case Left(error) => handleError(error)
case Right(value) => handleResult(value)
}</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>traits: sealed, ADT, mixins</h2>
<pre class="fragment"><code class="hljs scala">sealed trait Stack[+T]
case object Empty extends Stack[Nothing]
case class Top[+T](value: T, next: Stack[T])</code></pre>
<pre class="fragment"><code class="hljs scala">class Printer {
def result: List[Either[String, String]] = ???
def print: Unit = result.foreach(println)
}
trait ErrFilter { self: Printer => def result = super.result.filter(_.isRight) }
trait OutputLimiter { self: Printer => def result = super.result.take(20) }
new ResultPrinter with ErrFilter with OutputLimiter</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>immutable data structures</h2>
<pre class="fragment"><code class="hljs scala">case class Cheese(name: String, weightInG: Double)
val cheese1 = Cheese("Gouda", 12.6)
val cheese2 = cheese1.copy(name = "Cheddar")</code></pre>
<pre class="fragment"><code class="hljs scala">val list1 = "a" :: "b" :: "c" :: Nil
val list2 = list1 ++ List("d", "e", "f")</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>for comprehension</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs scala">for {
i <- List(1, 2, 3)
j <- List(4, 5, 6)
k <- List(7, 8, 9)
} yield i * j * k</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">List(1, 2, 3) flatMap { i =>
List(4, 5, 6) flatMap { j =>
List(7, 8, 9) map { k =>
i * j * k
}
}
}</code></pre></td>
</tr>
</table>
<aside class="notes">mention that Future in for is sequential by default</aside>
</section>
<section>
<h2>Implicits</h2>
<pre class="fragment"><code class="hljs scala">implicit val executionContext: ExecutionContext = ...
def asyncResult(implicit ec: ExecutionContext): Future[String] = ???</code></pre>
<pre class="fragment"><code class="hljs scala">implicit class StringDecorator(string: String) {
def cleanString = string.toLowerCase.trim
}
" MY STRING ".cleanString</code></pre>
<pre class="fragment"><code class="hljs scala">sealed trait Nat
case object Zero extends Nat
case class Succ[N <: Nat](value: N) extends Nat
implicit val natZero: Zero = Zero
implicit def natSucc[N <: Nat](implicit nat: N): Succ[N] = Succ(n)
implicitly[Succ[Succ[Succ[Zero]]]] // Succ(Succ(Succ(Zero)))</code></pre>
<pre class="fragment"><code class="hljs scala">def fun1[A](value: A)(implicit: fa: TypeClass[A]): A = ...
def fun2[A : TypeClass](value: A): A = ...</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>useful sources</h2>
<ul>
<li class="fragment"><a href="https://www.coursera.org/learn/progfun1">Functional Programming Principles in Scala</a> - Martin Odersky</li>
<li class="fragment"><a href="https://www.manning.com/books/functional-programming-in-scala">Functional Programming in Scala</a> - Paul Chiusano and Rúnar Bjarnason</li>
</ul>
</section>
</section>
<section>
<section>
<h1>FP design patterns</h1>
<aside class="notes">andthen, orelse, currying, functor, applicative, monad, arrow, lenses, ADT</aside>
</section>
<section>
<h2>function composition</h2>
<td><pre class="fragment"><code class="hljs scala">def parseXml(input: String): XmlDocument
def getUserDataNode(document: XmlDocument): XmlNode
def extractUserData(node: XmlNode): MyUserData
val extractUserDataFromInput = parseXml andThen
getUserDataNode andThen
extractUserData</code></pre>
<pre class="fragment"><code class="hljs scala">val handleSome: PartialFunction[Option[String], Unit] = {
case Some(result) => println(s"Result is $result")
}
val handleNone: PartialFunction[Option[String], Unit] = {
case None => println(s"There is no result")
}
val handleOption = handleSome orElse handleNone</code></pre></td>
<aside class="notes"></aside>
</section>
<section>
<h2>type classes</h2>
<pre class="fragment"><code class="hljs scala">trait MyTypeclass[A] {
def prettyString(printed: A): String
}</code></pre>
<pre class="fragment"><code class="hljs scala">implicit def typeclassForMap[K, V] = new MyTypeclass[Map[K, V]] {
def prettyString(printed: Map[K, V]) =
printed map { case (k, v) => s"($k -> $v)" } mkString "; "
}
implicit def typeclassForList[B] = new MyTypeclass[List[B]] {
def prettyString(printed: List[B]) = s"(${printed mkString ", "})"
}</code></pre>
<pre class="fragment"><code class="hljs scala">def printMe[A](toPrint: A)(implicit myTypeClass: MyTypeclass[A]) =
myTypeClass.prettyString(toPrint)
// alternatively
def printMe[A : MyTypeclass](toPrint: A) =
implicitly[MyTypeclass[A]].prettyString(toPrint)</code></pre>
<pre class="fragment"><code class="hljs scala">printMe(List(1, 2, 3)) // (1, 2, 3)
printMe(Map(1 -> 2, 3 -> 4)) // (1 -> 2); (3 -> 4)</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>semigroup</h2>
<pre class="fragment"><code class="hljs scala">trait Semigroup[A] {
def combine(a1: A, a2: A): A
}
implicit def listSemigroup[A] = new Semigroup[List[A]] {
def combine(a1: List[A], a2: List[A]): List[A] = a1 ++ a2
}</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>monoid</h2>
<pre class="fragment"><code class="hljs scala">trait Monoid[A] extends Semigroup[A] {
def zero: A
}
implicit def listMonoid[A] = new Semigroup[List[A]] {
def combine(a1: List[A], a2: List[A]): List[A] = a1 ++ a2
def zero: List[A] = List.empty
}</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>functor</h2>
<pre class="fragment"><code class="hljs scala">trait Functor[F[_]] {
def map[A, B](f: F[A], fun: A => B): F[B]
}
implicit def listFunctor[A] = new Functor[List] {
def map[A, B](f: List[A], fun: A => B): List[B] = f map fun
}</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>monad</h2>
<pre class="fragment"><code class="hljs scala">trait Monad[F[_]] extends Functor[F] {
def unit[A](a: A): F[A]
def flatMap[A, B](f: F[A], fun: A => F[B]): F[B]
}
implicit def listFunctor[A] = new Monad[List] {
def map[A, B](f: List[A], fun: A => B): List[B] = f map fun
def unit[A](a: A): List[A] = List(a)
def flatMap[A, B](f: List[A], fun: A => List[B]): List[B] = f flatMap fun
}</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>other useful concepts</h2>
<ul>
<li class="fragment">applicative functors</li>
<li class="fragment">arrows</li>
<li class="fragment">lenses</li>
<li class="fragment">prisms</li>
<li class="fragment">recursive schemes</li>
<li class="fragment">...</li>
</ul>
<aside class="notes"></aside>
</section>
<section>
<h2>useful sources</h2>
<ul>
<li class="fragment"><a href="https://www.manning.com/books/functional-programming-in-scala">Functional Programming in Scala</a> - Paul Chiusano and Rúnar Bjarnason</li>
<li class="fragment"><a href="https://leanpub.com/mastering-advanced-scala">Mastering Advanced Scala</a> - Denis Kalinin</li>
<li class="fragment"><a href="http://fsharpforfunandprofit.com/fppatterns/">Functional Programming Design Patterns</a> - Scott Wlaschin</li>
<li class="fragment">cats documentation</li>
<li class="fragment">tons of great papers</li>
</ul>
</section>
</section>
<section>
<section>
<h1>OOP design patterns</h1>
<aside class="notes">decoupling of data vs decoupling of behavior</aside>
</section>
<section>
<h2>abstract factory/factory method</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">class ConfigFactory {
MyConfig createConfig(
Boolean isDebug,
Set<String>: flags) { ... }
}
new ConfigFactory()
.createConfig(isDebug, flags);</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">def createConfig(
isDebug: Boolean,
flags: Set[String]): MyConfig = { ... }
createConfig(isDebug, flags)</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>builder</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">StringBuilder sb = new StringBuilder();
for (String line : fileContent) {
sb.append("line: ")
.append(line)
.append("\n");
}
String result = sb.toString();</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">val result =
fileContent.foldLeft(List.empty[String]) {
case (result, line) =>
result :+ "line: " :+ line
} mkString "\n"</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>builder</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">interface MyConfig {
Boolean isDebig();
Set<String> getFlags();
}
class MyConfigBuilder {
MyConfigBuilder setDebug(
Boolean isDebug) { ... }
MyConfigBuilder setFlags(
Set<String> flags) { ... }
MyConfig build() { ... }
}
new MyConfigBuilder()
.setDebug(true)
.setFlags(flags)
.build();</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">case class MyConfig(
isDebug: Boolean = false,
flags: Set[String] = Set.empty)
MyConfig().copy(isDebug = true, flags = flags)
// or MyConfig(isDebug = true, flags = flags)
// or MyConfig(isDebug = true).copy(flags = flags)
// etc</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>lazy initialization</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">interface X {
String getResult;
}
class LazyXProxy extends X {
private X inner = null;
private X getX() {
if (inner == null)
inner = ...;
return X;
}
String getResult() {
return getX.getResult();
}
}</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">trait X {
def getResult: String
}
lazy val lazyX: X = ...</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>singleton</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">class Singleton {
private static Singleton self = null;
private Singleton() {}
public static Singleton get() {
// this should be synchronized!
if (self == null)
self = new Singleton();
return self;
}
}</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">object Singleton</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>command object</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">interface Command {
void execute(target: Data);
}
class UpdateDataCommand extends Command {
void execute(target: Data) { ... }
}</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">trait Command extends (Data => Unit)
case object UpdateData extends Command {
def apply(target: Data): Unit = { ... }
}</code></pre></td>
</tr>
</table>
<aside class="notes">also mention servant</aside>
</section>
<section>
<h2>observer</h2>
<ul>
<li class="fragment">futures</li>
<li class="fragment">reactive programming in general</li>
<li class="fragment"><pre><code class="hljs scala">import rx._
val a = Var(1); val b = Var(2)
val c = Rx { a() + b() }
println(c.now) // 3
a() = 4
println(c.now) // 6</code></pre></li>
</ul>
<aside class="notes"></aside>
</section>
<section>
<h2>decorator</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">interface X {
String getResult();
}
class XDecorator extends X {
private X x;
XDecorator(X x) { this.x = x; }
String getResult() {
return x.getResult();
}
String getResultUpper() {
return getResult().toUpperCase();
}
}</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">trait X {
def getResult: String
}
implicit class XDecorator(x: X) {
def getResultUpper = x.getResult.toUpperCase
}</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>template method</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">abstract class SeriousOperations {
void doSeriousStuff() {
// doing things
String nextResult =
templateMethod(resultSoFar);
// doing more things
}
abstract String templateMethod(
String resultSoFar);
}
class SeriousImplementation
extends SeriousOperations {
String templateMethod(
String resultSoFar) { ... }
}</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">def seriousOperations(
template: String => String
): Unit = {
// doing things
val nextResult = template(resultSoFar);
// doing more things
}</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>strategy</h2>
<table>
<tr>
<td><pre class="fragment"><code class="hljs java">interface PrintStrategy {
String print(Data data);
}
class XmlPrintStrategy
extends PrintStrategy {
String print(Data data) { ... }
}
class JsonPrintStrategy
extends PrintStrategy {
String print(Data data) { ... }
}</code></pre></td>
<td><pre class="fragment"><code class="hljs scala">val printXMl: Data => String = ...
val printJson: Data => String = ...</code></pre></td>
</tr>
</table>
<aside class="notes"></aside>
</section>
<section>
<h2>interpreter</h2>
<pre class="fragment"><code class="hljs scala">sealed trait CalcDSL[T]
object CalcDSL {
case class AskForInput(question: String) extends CalcDSL[Int]
case class PrintResult(result: String) extends CalcDSL[Unit]
case class Add(i: Int, j: Int) extends CalcDSL[Int]
case class Mul(i: Int, j: Int) extends CalcDSL[Int]
class Ops {
def askForInput(question: String): Free[CalcDSL, Int] =
Free.liftF(AskForInput(question))
def printResult(result: String): Free[CalcDSL, Unit] =
Free.liftF(PrintResult(result))
def add(i: Int, j: Int): Free[CalcDSL, Int] = Free.liftF(Add(i: Int, j: Int))
def mul(i: Int, j: Int): Free[CalcDSL, Int] = Free.liftF(Mul(i: Int, j: Int))
}
}</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>interpreter</h2>
<pre class="fragment"><code class="hljs scala">val interpreter = new (CalcDSL ~> Id) {
def apply[A](in: CalcDSL[A]): A = in match {
case CalcDSL.AskForInput(question) => println(question); StdIn.readInt()
case CalcDSL.PrintResult(result) => println(result)
case CalcDSL.Add(i, j) => i + j
case CalcDSL.Mul(i, j) => i * j
}
}</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>interpreter</h2>
<pre class="fragment"><code class="hljs scala">val ops = new CalcDSL.Ops
val program: Free[CalcDSL, (Int, Int)] = for {
a <- ops.askForInput("a = ")
b <- ops.askForInput("b = ")
c <- ops.add(a, b)
_ <- ops.printResult(s"a + b = $c")
d <- ops.mul(a, b)
_ <- ops.printResult(s"a * b = $d")
} yield (c, d)
val (c, d) = program.foldMap(interpreter)</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>useful sources</h2>
<ul>
<li class="fragment"><a href="https://www.scala-lang.org/old/sites/default/files/FrederikThesis.pdf">Scala & Design Patterns</a> - Fredrik Skeel Løkke</li>
<li class="fragment">and tons of articles</li>
</ul>
</section>
</section>
<section>
<section>
<h1>macros and type-level</h1>
</section>
<section>
<h2>example of macro generated code</h2>
<pre class="fragment"><code class="hljs scala">import play.api.libs.json._
case class Resident(name: String, age: Int, role: Option[String])
implicit val residentFormat = Json.format[Resident] // code generated by macro
val resident = Resident(name="Fiver", age=4, role=None)
val residentJson: JsValue = Json.toJson(resident)
val jsonString: JsValue = Json.parse("""{
| "name" : "Fiver",
| "age" : 4
|}""".stripMargin)
val residentFromJson: JsResult[Resident] = Json.fromJson[Resident](jsonString)</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>example of type-level achievements</h2>
<pre class="fragment"><code class="hljs scala">import io.circe._
import io.circe.generic.auto._
import io.circe.parser._
import io.circe.syntax._
sealed trait Foo
case class Bar(xs: List[String]) extends Foo
case class Qux(i: Int, d: Option[Double]) extends Foo
val foo: Foo = Qux(13, Some(14.0))
foo.asJson.noSpaces
// res0: String = {"Qux":{"i":13,"d":14.0}}
decode[Foo](foo.asJson.spaces4)
// res1: Either[io.circe.Error,Foo]</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>useful sources</h2>
<ul>
<li class="fragment"><a href="http://docs.scala-lang.org/overviews/macros/overview.html">Macros</a> - Scala Documentation</li>
<li class="fragment"><a href="http://underscore.io/books/shapeless-guide/">The Type Astronaut’s Guide to Shapeless</a> - Dave Gurnell</li>
</ul>
</section>
</section>
<section>
<section>
<h1>don't port (bad) Java practices to Scala</h1>
<aside class="notes">solid, null, deep inheritance, no need of mocks</aside>
</section>
<section>
<h2>for DI rely on types instead of reflection</h2>
<pre class="fragment"><code class="hljs scala">bind(classOf[Dependency])
.to(classOf[DependencyImpl])
class Component @Inject (deps: Dependency) {}
// what if Dependency is abstract
// and you forgot to configure:
// - providers,
// - bindings,
// - etc?
// why not simply?
new Component(dependency)</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>there are no statements, only expressions</h2>
<p>and so we never need to use <b>return</b></p>
<pre class="fragment"><code class="hljs scala">// no need to use var, return
val r1 = if (condition) "some value" else "some other value"
val r2 = for (i <- 0 to 100)
yield i * 2
def ok = List(1, 2, 3).foldLeft(0) { _ + _ } // 6
def invalid = List(1, 2, 3).foldLeft(0) { return _ + _ } // 1 !!!</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>there are no statements, only expressions</h2>
<p>and so we never need to use <b>return</b></p>
<pre class="fragment"><code class="hljs scala">// no need to use var, return
val r1 = if (condition) "some value" else "some other value"
val r2 = for (i <- 0 to 100)
yield i * 2
def ok = List(1, 2, 3).foldLeft(0) { _ + _ } // 6
def invalid = List(1, 2, 3).foldLeft(0) { return _ + _ } // 1 !!!</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>using exception for error handling</h2>
<pre class="fragment"><code class="hljs scala">def doSomething: Int =
if (condition) 1024
else throw new Exception("invalid result")
val result = Try(doSomething) // we need to handle exception
// (and be aware that the function throws it)</code></pre>
<pre class="fragment"><code class="hljs scala">def doSomething: Either[String, Int] =
if (condition) Right(1024)
else Left("invalid result")
val result = doSomething // type indicate 2 possible results</code></pre>
<pre class="fragment"><code class="hljs scala">def doSomething: Validated[String, Int] =
if (condition) Validated.valid(1024)
else Validated.invalid("invalid result")
val result = doSomething // possible error indicated by type even better</code></pre>
<aside class="notes"></aside>
</section>
<section>
<h2>other offenders</h2>
<ul>
<li class="fragment">deep hierarchies - difference in behavior can injected as functions</li>
<li class="fragment">heavy violations of SOLID/DRY/YAGNI/... - never good</li>
<li class="fragment">using mutable state in not-exceptional cases - a lot of optimizations can be achieved by changing used algorithms and data structures, and making data more CPU-cache friendly</li>
<li class="fragment">using nulls - exception: interop with Java</li>
</ul>
<aside class="notes"></aside>
</section>
</section>
<section>
<h2>Summary</h2>
<ul>
<li class="fragment">language and libraries give us greater power of expressions using functions</li>
<li class="fragment">harvesting the whole power of a language/paradigm requires learning some design patterns it uses</li>
<li class="fragment">Java comes with some inherited baggage - in Scala we can avoid some solutions that Java just cannot get rid of</li>
</ul>
</section>
<section>
<h2>Questions?</h2>
</section>
<section>
<h2>Thank you!</h2>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="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: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>