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# meetup scala class
## part 1
## 11 June 2014
---
Visit the repo on github:
```
https://github.com/meetup/meetup-scala-class
```
Open a terminal and clone the repository:
```
$ git clone git@github.com:meetup/meetup-scala-class.git
$ cd meetup-scala-class
```
This presentation is located at *pdf/class1.pdf*.
```
$ open pdf/class1.pdf
```
---
Launch the REPL:
```
$ ./sbt console
...
Welcome to Scala version 2.10.4 ...
Type in expressions to have them evaluated.
Type :help for more information.
scala> "Hello, world"
res0: String = Hello, world
```
---
Scala ignores lines beginning with `//`:
```scala
// four thousand years ago Babylonians knew
// that 22/7 was a good approximation of pi.
22.0 / 7.0
```
---
We've got a calculator:
```scala
1 + 2 + 3 + 4 // addition
1000 - 245 // subtraction
7.25 * 35 // multiplication
22980.0 / 52 // division [1]
44 % 7 // remainder
// [1] compare to 22980 / 52
```
---
Values:
* Immutable, i.e. they don't change.
* Types describe the possibilities/shape of data
* Values can either be:
+ simple (numbers, text, true/false, etc.)
+ complex (pairs, lists, dictionaries, etc.)
---
Naming values:
```scala
val minimumWage = 7.25
val hoursPerWeek = 35
val weeksPerYear = 52
minimumWage * hoursPerWeek * weeksPerYear
// res0: Double = 13195.0
15.0 * hoursPerWeek * weeksPerYear
// res1: Double = 27300.0
```
---
Functions:
```scala
def wages(wage: Double, weeklyHours: Int) =
wage * weeklyHours * weeksPerYear
wages(7.25, 35)
wages(11.0, 35)
wages(13.0, 35)
wages(17.0, 35)
```
---
More functions:
```scala
def pythagoras(x: Double, y: Double): Double =
math.sqrt(x * x + y * y)
pythagoras(1.0, 1.0)
pythagoras(3.0, 4.0)
pythagoras(1.0, 2.0)
```
---
How do we know what values are capable of?
# *Types!*
(We've already seen types; they come "after the colon")
---
Each type provides methods which we can call. E.g.:
```scala
// + is a method on Double
// so is .abs
def f(x: Double, y: Double) = (x + y).abs
```
Let's look at some useful types and methods...
---
Number Types (`Int`, `Double`, etc.)
* arithmetic (`+` `-` `*` `/` `%`)
`x + y * z`
* comparisons (`==` `!=` `>` `>=` `<` `<=` `max` `min`)
`x <= y`
* misc (`.abs` `.ceil` `.floor` `.round` `.signum`, `.toString`)
`(x / y).round`
---
Number types (continued)
* `Int` is useful for reasonably-sized whole values.
* `Double` is useful for most fractional values.
(The `math` package provides functions like `pow`)
(There are other more exotic number types as well!)
---
Text (`String`, `Char`, etc.)
```scala
val line = "Defines some text."
val char = 's' // a single character
val fancy = s"We can interpolate: $line"
// fancy: String = We can interpolate: Defines some text.
```
(`String` is the same as `java.lang.String`)
---
Text (continued)
```scala
val s = "batman"
s.length // res0: Int = 6
s.toUpperCase // res1: String = BATMAN
s.startsWith("cat") // res2: Boolean = false
s.replace("b", "w") // res3: String = watman
s + " and robin" // res4: String = batman and robin
```
---
Boolean
Contains only two values: `true` and `false`.
```scala
val x = true
val y = false
!x // not: since x is true, (!x) is false
x && y // and: since y is false (x && y) is false
x || y // or: since x is true (x || y) is true
if (y) 1 else 0 // if/else: since y is false, return 0
```
(technically, if/else is syntax, not a method call)
---
We can already do a lot with this!
```scala
// concatenate s to itself n times
def repeatConcat(s: String, n: Int): String =
if (n <= 0) "" else s + repeatConcat(s, n - 1)
// calculate interest on p compounded n times per year
def interest(p: Double, rate: Double, years: Double, n: Double) =
p * math.pow(1 + (rate / n), num * years)
```
---
Let's look at `repeatConcat` more closely
```scala
repeatConcat("cat", 3)
"cat" + repeatConcat("cat", 2)
"cat" + "cat" + repeatConcat("cat", 1)
"cat" + "cat" + "cat"
"catcatcat"
```
This is an example of recursion.
---
We can write the method in a different way if we want
```scala
def repeatConcat2(s: String, n: Int): String = {
@tailrec def loop(sofar: String, i: Int): String =
if (i < n) loop(s + sofar, i + 1) else sofar
loop("", 0)
}
```
Let's dissect that a bit...
---
The `{ ... }` define a block.
Blocks can be used to:
* allow "inner" method/value definitions
* support pattern matching
* allow writing "imperative" code
---
```scala
def repeatConcat2(s: String, n: Int): String = {
def loop(sofar: String, i: Int): String =
if (i < n) loop(s + sofar, i + 1) else sofar
loop("", 0)
}
repeatConcat("dog", 2)
loop("", 0)
loop("dog", 1)
loop("dogdog", 2)
loop("dogdogdog", 3)
"dogdogdog"
```
---
The previous strategy is called "tail recursion"
* Compiles to a very efficient representation
* Often faster than "normal" recursion
* Less general (not all recursive methods can be tail-recursive)
---
Tuples
We can group several values together to create a tuple:
```scala
val nyc = (40.7127, -74.0059)
// nyc: (Double, Double) = (40.7127,-74.0059)
val poem = ("The Raven", Poe", 1845)
// poem: (String, String, Int) = (The Raven,Poe,1845)
poem._3
// res1: Int = 1845
```
---
* Any types can be combined in a tuple.
* `(Int, Int)` is a type (it holds two `Int` values).
* Access positions with `._1`, `._2`, `._3`, etc.
* We can also "destructure" tuples (take them apart).
```scala
val (title, author, year) = poem
// title: String = The Raven
// author: String = Poe
// year: Int = 1845
```
---
Case classes
Like tuples, but with fixed names/types.
```scala
case class Point(lat: Double, lon: Double)
case class Poem(title: String, author: String, year: Int)
val nyc = Point(40.7127, -74.0059)
val poem = Poem("The Raven", Poe", 1845)
```
---
Type-checking
Because our types are fixed, we can catch mistakes.
```scala
val wrong = Point("40.7127", "-74.0059")
// <console>:9: error: type mismatch;
// found : String("40.7127")
// required: Double
// val wrong = Point("40.7127", "-74.0059")
```
---
Destructuring
Case classes can be destructured just like tuples.
```scala
val Point(lat, lon) = nyc
val Poem(title, author, year) = poem
```
In fact, destructuring is a form of *pattern matching*
---
Pattern Matching
Here's a method we might choose to write:
```scala
def isModernist(poem: Poem): Boolean = {
val Poem(title, author, year) = poem
year >= 1890
}
```
---
We can use the `match` statement to do the same thing:
```scala
def isModernist(poem: Poem): Boolean =
poem match {
case Poem(_, _, year) =>
year >= 1890
}
```
(In this case `_` avoids binding names.)
---
Unlike `val`, `match` supports conditional logic:
```scala
def score(poem: Poem): Double =
poem match {
case Poem("The Raven", _, _) => 0.9
case Poem(_, "Eliot", _) => 0.7
case Poem(_, "Poe", _) => 0.5
case Poem(_, _, y) if y == 1923 => 0.3
case _ => 0.2
}
```
---
We can even use pattern matching on simple values:
```scala
def synesthesia(n: Int): String =
n match {
case 3 => "yellow"
case 5 => "red"
case 7 => "blue-green"
case _ => "grey"
}
```
---
Another example:
```scala
def ordinal(n: Int): String = {
def suffix(n: Int): String = n match {
case 1 => "st"
case 2 => "nd"
case 3 => "rd"
case x if x <= 20 => "th"
case x if x >= 100 => suffix(x % 100)
case x => suffix(x % 10)
}
n.toString + suffix(n)
}
```
---
List
Often we want to talk about a list of values:
```scala
val poems: List[Poem] =
Poem("The Raven", "Poe", 1845) ::
Poem("Jabberwocky", "Carroll", 1871) ::
Poem("The Waste Land", "Eliot", 1922) ::
Nil
```
(The `Nil` value is an empty list.)
---
Lists can be prepend to with the `::` method.
```scala
val addedPoems: List[Poem] =
Poem("Howl", "Ginsberg", 1955) ::
Poem("Tulips", "Plath", 1966) ::
poems // previously-defined poems
```
We can also take a list apart via pattern matching.
```scala
def isListEmpty(nums: List[Int]): Boolean =
nums match {
case Nil => true
case _ => false
}
```
---
Using a simple recursive method, we can sum a list.
```scala
def sumList(ns: List[Int]): Int = ns match {
case Nil => 0
case first :: rest => first + sumList(rest)
}
sumList(Nil) // res0: Int = 0
sumList(1 :: 2 :: 3 :: Nil) // res1: Int = 6
```
---
Let's see an example in more detail:
```scala
def sumList(ns: List[Int]): Int = ns match {
case Nil => 0
case first :: rest => first + sumList(rest)
}
sumList(13 :: 45 :: 8 :: Nil)
13 + sumList(45 :: 8 :: Nil)
13 + 45 + sumList(8 :: Nil)
13 + 45 + 8 + sumList(Nil)
13 + 45 + 8 + 0
66
```
---
REPL tips:
* Add your solutions to `exercises/foo.scala`
* `:load exercises/foo.scala` will (re)load your code
* Test your solutions in the REPL
* You can also add test cases to your file
---
Exercises
```scala
// 1. Jane works 45 hours a week at $15.5/hour.
// What are her yearly earnings?
// 2. Assuming overtime work gets paid at 1.5
// times the normal rate, what are Jane's
// yearly earnings?
// 3. Write a method that generalizes #2
def payWithOvertime(wage: Double, hours: Double): Double = ???
// 4. How many hours per week must Jane work
// to earn $42,000 per year?
```
---
Exercises
```scala
// 5. Write a method that given a name (e.g. "Albert"),
// produces a string of greeting (e.g. "Hello Albert").
def greet(name: String): String = ???
// 6. Modify the method to that produces a different greeting
// for your team members' names (e.g. "Salutations Brian").
// 7. Write a method that returns the number of names in a list.
def numNames(names: List[String]): Int = ???
// 8. Write a method that determines if the given name exists
// in a list of names.
def locate(given: String, names: List[String]): Boolean = ???
```
---
Exercises
```scala
// 9. Produce a single greeting for a list of names
def greet3(names: List[String]): String = ???
greet3(Nil)
// res0: String = "Hello!"
greet3("Alice" :: Nil)
// res1: String = "Hello Alice"
greet3("Alice" :: "Bob" :: Nil)
// res1: String = "Hello Alice and Bob"
greet3("Alice" :: "Bob" :: "Cate" :: Nil)
// res2: String = "Hello Alice, Bob, and Cate"
```
---
Exercises
```scala
// 10. Reimplement sumList but using Double instead of Int.
// 11. Rewrite sumList method to use tail recursion.
def sumList(ns: List[Double]): Double = {
def loop(sofar: Double, xs: List[Double]): Double = ???
loop(0.0, ns)
}
// 12. Implement a method to find the length of the list
def listLength(xs: List[Double]): Int = ???
// 13. Implement a way to reverse a list
def reverseList(xs: List[Double]): List[Double] = ???
// 14. Find the minimum value in a list
def minList(xs: List[Int], min: Int): Int = ???
```
---
```scala
// 15. Find the minimum and maximum values in a list
def minMax(xs: List[Int], min: Int, max: Int): (Int, Int) = ???
// 16. Return only multiples of 3 (use % operator)
def onlyDivisibleBy3(xs: List[Int]): List[Int] = ???
// 17. Return whether n is a multiple of any of
// the given divisors.
def divides(n: Int, divisors: List[Int]): Boolean = ???
// divides(3, Nil) // false
// divides(3, 3 :: Nil) // true
// divides(3, 2 :: Nil) // false
// divides(20, 3 :: 7 :: Nil) // false
// divides(20, 5 :: 7 :: Nil) // true
// 18. Return only multiples of the divisors.
def divBy(xs: List[Int], divisors: List[Int]): List[Int] = ???
```
---
Exercises
```scala
// 19. Compute the mean (average) of a list
// (Hint: look at sumList and listLength)
def mean(xs: List[Double]): Double = ???
// 20. If you didn't already, solve #19 using
// a single tail-recursive inner function.
// 21. (Extra credit) Standard deviation is defined
// as the square root of the average distance
// from the average.
def stdDev(xs: List[Double]): Double = ???
```
---
Exercises
```scala
// 22. The Fibonacci sequence is defined as:
// fib(0) = 0
// fib(1) = 1
// fib(n) = f(n-1) + f(n-2)
// Implement it using recursion (fib(20) is 10946).
def fib(n: Int): Int = ???
// 23. (Extra credit) The traditional recursive solution
// to #10 will evaluate f(n-2) twice, once on the (n)
// step and once on the (n-1) step.
//
// Implement a better version using an inner method.
```
---
```scala
// 24. Your fib function probably only works for
// values from 0 though 46 (for larger values
// it likely produces negative numbers).
//
// Implement fib10(n), a method that returns that
// last digit of fib(n), and which does not have
// this problem.
def fib10(n: Int): Int = ???
// fib10(51) = 4
// fib10(503) = 7
// fib10(5007) = 8
// fib10(50003) = 7
```
---
You're done! Great!
You can:
* Ask someone to look over your work
* Compare notes with someone else
* Pair with someone who is still working