-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
458 lines (417 loc) · 13 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Scala for Python developers</title>
<meta name="description" content="Slides for talk @ Montreal Python 43">
<meta name="author" content="Alexandre Bergeron">
<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">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/solarized.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Scala for Python Developers</h1>
<p>
Alexandre Bergeron
<br>
Backend Developer @ Wajam
<br>
@AlexBergeron2
</p>
<br>
<p>
Montreal Python 43 - Artistic Baboon
<br>
February 10, 2014
</p>
</section>
<section>
<h2>About me</h2>
<ul>
<li>Bachelor degree in Software Engineering from ETS April 2012</li>
<li>Started coding in Python in 2007</li>
<li>Started working at Wajam in April 2012</li>
<li>Started coding in Scala same month</li>
</ul>
</section>
<section>
<h2>What is Scala</h2>
<ul>
<li>Statically-typed, object-oriented, functional & imperative language</li>
<li>Compiles to the JVM - Can call Java libraries transparently</li>
<li>Type inference - Types are deducted, no need to specify them</li>
<li>Built for concurrency - parrallel collections, actors frameworks, asynchronous Futures</li>
</ul>
</section>
<section>
<section>
<h2>Some basics</h2>
<p>Values and variables - strong focus on immutability</p>
<pre><code>
// Mutable
var mutableVariable = "Test" // Type String infered
mutableVariable = "SomethingElse"
// Immutable
val immutableValue = 42 // Type Int infered
//immutableValue = 0 // error: reassignment to val
// We've got Java Arays - we just don't use them a lot
val someArray = Array("Hello", "World")
someArray(1) = "Scala" // They're mutable
println(someAray.mkString(" "))
</code></pre>
</section>
<section>
<h2>Some basics</h2>
<p>Control statements</p>
<pre><code>
val meetup = "Montreal Python"
if (meetup.contains("Python")) {
println("You can see me")
} else {
// Dead code here
}
// if can also return a value, or be a one-liner
val foo = if (presenterRunningOutOfImagination) bar else baz
try {
// Something that might throw an exception
} catch {
case e => // Handle that exception
}
</code></pre>
</section>
<section>
<h2>Some basics</h2>
<p>More control statements</p>
<pre><code>
// Scala also has while and do...while loops
// But they're seen as bad practice and used mostly for optimizations
while (someCondition) {
executeSomeOtherTasks()
}
for (i <- 1 to 5) println(i)
// Syntactic sugar for
(1 to 5).foreach(i => println(i))
</code></pre>
</section>
<section>
<h2>Some basics</h2>
<p>Function declaration</p>
<pre><code>
// Our first function
def multiply(x: Int, y: Int): Int = x * y
multiply(2, 3)
// Unit-returning function
def show(i: Int): Unit = {
println("show(" + i + ")")
}
// Or, as an anonymous function
def anon = (x: Int, y, Int) => x * y // Type Infered
anon(2, 3)
// Functions can take functions as a parameter
def applyOperation(value: Int, oper: Int => Int): Int = oper(value)
</code></pre>
</section>
<section>
<h2>Some basics</h2>
<p>Object-oriented - class, object, extends</p>
<pre><code>
abstract class Foo {
def a: String
}
// Classes can only inherit from one other class
class Bar extends Foo {
def a = "Bar"
}
// Singleton objects - can behave like a class
object Baz extends Foo {
def a = "Baz"
}
println(baz.a)
// Companion objects can also be created for existing classes
</code></pre>
</section>
</section>
<section>
<section>
<h2>Similarities to Python</h2>
<h3>Collections</h3>
<p>Python</p>
<pre><code>
# Lists - Ordered, duplicates allowed
a_list = [1, 2, 3, 4]
# Tuples - Ordered, fixed
a_tuple = (1, 2, 3)
a, b, c = a_tuple
# Sets - Unordered, duplicates not allowed
a_set = {1, 2, 3, 3} # {1, 2, 3}
# Dicts - Binds keys to values
a_dict = {"one": 1, "two": 2}
</code></pre>
</section>
<section>
<h2>Similarities to Python</h2>
<h3>Collections</h3>
<p>Scala</p>
<pre><code>
// Collections are immutable by default
// Mutable versions available
// Seqs - Ordered, duplicates allowed
val seq = Seq(1, 2, 3, 4)
val list = List(1, 2, 3, 4) // 1 :: 2 :: 3 :: 4 :: Nil
// Tuples - Ordered, fixed
val tuple = (1, 2, 3)
val (a, b, c) = tuple
// Sets - Unordered, duplicates not allowed
val set = Set(1, 2, 3, 3) // Set(1, 2, 3)
// Map - Binds keys to values
val map = Map("one" -> 1, "two" -> 2)
</code></pre>
</section>
<section>
<h2>Similarities to Python</h2>
<h3>List comprehensions</h3>
<p>Python</p>
<pre><code>
[x * x for x in range(1, 4)]
# [1, 4, 9]
[x for x in range(1, 10) if x % 3 == 0]
# [3, 6, 9]
[(x, y) for x in range(1,5) for y in range(x, 5) if x + y == 5]
# [(1, 4), (2, 3)]
</code></pre>
</section>
<section>
<h2>Similarities to Python</h2>
<h3>For comprehensions</h3>
<p>Scala</p>
<pre><code>
for (x <- 1 to 3) yield x * x
// Vector(1, 4, 9)
for (x <- 1 to 10 if x % 3 == 0) yield x
// Vector(3, 6, 9)
for {
x <- 1 until 5
y <- x until 5
if x + y == 5
} yield (x, y)
// Vector((1,4), (2,3))
</code></pre>
</section>
<section>
<h2>Similarities to Python</h2>
<h3>Functional programming - map, filter</h3>
<p>Python</p>
<pre><code>
map(lambda x: x * x, range(1, 4))
# [1, 4, 9]
filter(lambda x: x % 3 == 0, range(1,10))
# [3, 6, 9]
from itertools import combinations
def sum_is_five(tuple):
x, y = tuple
return x < y and x + y == 5
filter(sum_is_five, combinations(range(1, 5), 2))
# [(1, 4), (2, 3)]
</code></pre>
</section>
<section>
<h2>Similarities to Python</h2>
<h3>Functional programming - map, filter</h3>
<p>Scala</p>
<pre><code>
(1 to 3).map(x => x * x)
// Vector(1, 4, 9)
(1 to 10).filter(_ % 3 == 0) // Implicit parameter
// Vector(3, 6, 9)
(1 until 5).combinations(2).filter(_.sum == 5).toList
// List(Vector(1, 4), Vector(2, 3))
// Or, closer to what the for-comprehension executes
def sumIsFive(t: (Int, Int)) = {
val (x, y) = t
x + y == 5
}
(1 to 5).flatMap(x => (x to 5).map((x, _))).filter(sumIsFive)
// Vector((1,4), (2,3))
</code></pre>
</section>
</section>
<section>
<section>
<h2>Distinctions</h2>
<p>Pattern matching</p>
<pre><code>
// Looks like switch from Java
def sign(n: Int) = n match {
case 0 => "zero"
// Supports guards
case n if < 0 => "negative"
case _ => "positive
}
</code></pre>
</section>
<section>
<h2>Distinctions</h2>
<p>Pattern matching</p>
<pre><code>
// Gets even better with extractors
// Like when pattern matching with lists
def sum(lst: List[Int]): Int = lst match {
case head :: tail => head + sum(tail)
case Nil => 0
}
</code></pre>
</section>
<section>
<h2>Distinctions</h2>
<p>Option - Avoiding nulls</p>
<pre><code>
// Option[A] type has two possible values
// Some(value: A)
// None
// This allows to avoid nulls and NullPointerException
def greet(name: Option[String]): Unit = {
println("Hello " + name.getOrElse("world"))
}
greet(Some("Python"))
greet(None)
</code></pre>
</section>
<section>
<h2>Distinctions</h2>
<p>Option - Avoiding nulls</p>
<pre><code>
// Option also fits into pattern matching
def respond(result: Option[String]): Unit = result match {
case Some(r) => println("Response sent: " + r)
case None => println("No response sent")
}
</code></pre>
</section>
<section>
<h2>Distinctions</h2>
<p>Try - Avoiding calls to try</p>
<pre><code>
// Like Option, Try[A] has two possible values
// Success(value: A)
// Failure(t: Throwable)
// And it can be used with pattern matching
Try(4 / 2) // Success(2)
Try(1 / 0) // Failure(java.lang.ArithmeticException: / by zero)
</code></pre>
</section>
<section>
<h2>Distinctions</h2>
<p>case class - Easy way to create class that integrates with Pattern Matching</p>
<pre><code>
case class Person(name: String, city: String, job: Option[String])
// Every parameters in that constructor is now an immutable field
def makeStringForPerson(p: Person) = {
println(p.name + " - " p.job,getOrElse("unemployed") + " in " + p.city)
}
// You can also easily clone a case class
def moveTo(p: Person, c: String) = p.copy(city = c)
// As you'll see, you can also have case objects
</code></pre>
</section>
<section>
<h2>Distinctions</h2>
<p>Traits - Allows to inherit from multiple traits with implementations</p>
<pre><code>
trait A {
def toImplement(): Int
}
trait B {
val value = 42
def implemented() = value
}
class C extends A with B {
def toImplement() = implemented()
}
</code></pre>
</section>
<section>
<h2>Distinctions</h2>
<p>Traits - Allows to inherit from multiple traits with implementations</p>
<pre><code>
// sealed = you can't implement this trait outside of this file
// The + in front of means it's covariant.
sealed trait Option[+A] {
def getOrElse(value: A) = this match {
case Some(v) => v
case None => value
}
}
case class Some[+A](value: A) extends Option[A]
case object None extends Option[Nothing] // Nothing = Bottom type
</code></pre>
</section>
</section>
<section>
<h2>Points of contention</h2>
<ul>
<li class="fragment">Static typing vs Dynamic Typing</li>
<li class="fragment">Code nesting vs Zen of Python (PEP-20)</li>
<li class="fragment">Readability from line conciseness and explicit types vs short methods and naming things </li>
<li class="fragment">More than one way to do it vs only one way to do it</li>
</ul>
</section>
<section>
<h2>Read more about Scala</h2>
<ul>
<li>Scala School from Twitter - <a href="http://twitter.github.io/scala_school/">http://twitter.github.io/scala_school/</a></li>
<li>Functional Programming Principles in Scala on Coursera - <a href="https://www.coursera.org/course/progfun">https://www.coursera.org/course/progfun</a></li>
<li>Scala for the Impatient by Cay Horstmann - <a href="http://horstmann.com/scala/">http://horstmann.com/scala/</a></li>
<li>Programming in Scala by Martin Odersky, Lex Sppon and Bill Venners - <a href="http://www.artima.com/shop/programming_in_scala_2ed">http://www.artima.com/shop/programming_in_scala_2ed</a></li>
</ul>
</section>
<section>
<h1>Questions?</h1>
<h3>Thanks for listening</h3>
<p><small>Made with reveal.js</small></p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: false,
progress: false,
slideNumber: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: 'fade', // default/cube/page/concave/zoom/linear/fade/none
// Parallax scrolling
// parallaxBackgroundImage: 'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg',
// parallaxBackgroundSize: '2100px 900px',
// Optional libraries used to extend on reveal.js
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, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>