-
Notifications
You must be signed in to change notification settings - Fork 6
/
Part2.txt
357 lines (275 loc) · 7.44 KB
/
Part2.txt
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
| \*\gScala: The Swiss Army Language
| \cwith Nathan Dotz
https://git.io/oscon16-scala
:bird: \ctwitter: @nathandotz
:octopus: \mgithub: @sleepynate
:sparkling_heart: \yemployer: @DetroitLabs
---
| \*\gPart 2: Types, and Patterns
---
| \*\gScala is type-safe (mostly)
scala> ```var a = 5```
a: Int = 5
scala> ```a = "i think it's going to break"```
<console>:8: error: type mismatch;
found : String("i think it\'s going to break")
required: Int
a = "i think it's going to break"
^
---
| \*\gType inference
scala> ```var b = 5.0```
b: Double = 5.0
scala> ```var d = '5'```
d: Char = 5
scala> ```var e = true```
e: Boolean = true
scala> ```var f = null```
f: Null = null
scala> ```var g = List('x, 'y, 'z)```
g: List[Symbol] = List('x, 'y, 'z)
---
scala> ```var h = List('x, 'y', 'z)```
h: List[Any] = List('x, y, 'z)
scala> ```h.head < h(1)```
<console>:9: error: value < is not a member of Any
h.head < h(1)
^
Usually it's good but sometimes it can trip you up
---
| \*\gType Annotations and Declarations
scala> ```val x = 1```
x: Int = 1
scala> :t x
Int
The REPL shows us the type we've declared with an \yannotation
---
| \*\gType Annotations and Declarations
scala> ```val addAnotherOne = (x:Int) => x + 1```
addAnotherOne: Int => Int = <function1>
We've seen the type of lambdas too
and the fat arrow tells us it's a function
---
| \*\gType Annotations and Declarations
scala> ```def revMe(s: String) = s.reverse```
revMe: (s: String)String
scala> ```def thereAndBack(t: String, b: String) = t + b.reverse```
thereAndBack: (t: String, b: String)String
Methods just have the return type appended
---
| \*\gType Annotations and Declarations
scala> ```def foo(f: Int => String => Boolean => Double) = f.apply(5)```
foo: (f: Int => (String => (Boolean => Double)))String => (Boolean => Double)
Can you read this one yet?
---
| \*\gAscripting type annotations
scala> ```val poo:String = "the bear, of course!"```
poo: String = the bear, of course!
--
scala> ```val poo:Any = "the bear, of course!"```
poo: Any = the bear, of course!
scala> ```poo.reverse```
<console>:9: error: value reverse is not a member of Any
poo.reverse
^
---
| \*\gType parameters
scala> ```val a = List(1, 2, 3)```
a: List[Int] = List(1, 2, 3)
--
scala> ```(1 to 5).toList```
res5: List[Int] = List(1, 2, 3, 4, 5)
scala> ```Set('a', 'b', 'c').toList```
res6: List[Char] = List(a, b, c)
---
| \*\gType parameters
scala> ```def gimmeThreeOf[A](x: A):List[A] = List(x, x, x)```
gimmeThreeOf: [A](x: A)List[A]
scala> ```gimmeThreeOf(5)```
res7: List[Int] = List(5, 5, 5)
scala> ```gimmeThreeOf('r')```
res8: List[Char] = List(r, r, r)
---
| \*\gPattern Matching
---
| \*\gPattern Matching
```
def magicNumber(n: Int) = n match {
case 3 => "IT'S THE MAGIC NUMBER!"
case _ => "just an ordinary number"
}
```
magicNumber: (n: Int)String
--
scala> ```magicNumber(4)```
res0: String = just an ordinary number
scala> ```magicNumber(5)```
res1: String = just an ordinary number
scala> ```magicNumber(3)```
res2: String = IT'S THE MAGIC NUMBER!
---
| \*\gPattern Matching
```
def failMatch(a: Any) = a match {
case _:Int => "int!"
}
```
failMatch: (a: Any)String
scala> ```failMatch(1)```
res3: String = int!
scala> ```failMatch(5.5)```
scala.MatchError: 5.5 (of class java.lang.Double)
// …
---
| \*\gPattern Matching
```
def magicNumber(n: Int) = n match {
case 3 => "IT'S THE MAGIC NUMBER!"
case x => x + " is just an ordinary number"
}
```
magicNumber: (n: Int)String
scala> ```magicNumber(3)```
res5: String = IT'S THE MAGIC NUMBER!
scala> ```magicNumber(436782)```
res6: String = 436782 is just an ordinary number
---
| \*\gPattern Matching
```
def triangular(n: Int):Int = n match {
case 1 => 1
case x => x + triangular(x - 1)
}
```
triangular: (n: Int)Int
--
scala> ```triangular(10)```
res0: Int = 55
scala> ```triangular(32843)```
res1: Int = 539347746
---
| \*\gDecomposition
```
("first", "second") match {
case (f, s) => f + " is first and next comes " + s
}
```
We can decompose types to extract their parts
---
| \*\gDecomposition
```
def vectorAdd(first: (Int, Int), second: (Int, Int)) =
(first._1 + second._1, first._2 + second._2)
```
vectorAdd: (first: (Int, Int), second: (Int, Int))(Int, Int)
--
scala> ```vectorAdd((3,0), (1,4))```
res0: (Int, Int) = (4,4)
---
| \*\gDecomposition
```
def vectorAdd(first: (Int, Int), second: (Int, Int)) =
(first, second) match {
case ((x1, y1), (x2, y2)) => (x1+x2, y1+y2)
}
```
vectorAdd: (first: (Int, Int), second: (Int, Int))(Int, Int)
--
scala> ```vectorAdd((3,0), (1,4))```
res1: (Int, Int) = (4,4)
---
| \*\gDecomposition
```
def head[A](l:List[A]) = l match {
case Nil => throw new Exception("Oops!")
case h :: _ => h
}
```
scala> ```head(List(1,2,3))```
res7: Int = 1
We can also decompose on many constructors!
---
| \*\gDecomposition
scala> ```val tuples = List((1, 2), (3, 4), (5, 6))```
res3: List[(Int, Int)] = List((1,2), (3,4), (5,6))
scala> ```for ((x,y) <- tuples) yield x+y```
res4: List[Int] = List(3, 7, 11)
---
| \*\gDecomposition and aliases
```
(1, "hurray!") match {
case w@(i:Int, s:String) => w + " is made of a " +
i + " and a " + s
}
```
res0: String = (1,hurray!) is made of a 1 and a hurray!
---
| \*\gGuards
```
def mySteps(steps:Int) =
if (steps < 2500) "you aren't even trying"
else if (steps < 5000) "that is pitiful"
else if (steps < 7500) "ok but still walk more"
else "hey you walked. good for you."
```
This method for our FatBot personal pedometer can
be refactored to use \ymatch\s's guard statements.
---
| \*\gGuards
```
def mySteps(steps:Int) = steps match {
case s if (s < 2500) => "you aren't even trying"
case s if (s < 5000) => "that is pitiful"
case s if (s < 7500) => "ok but still walk more"
case _ => "hey you walked. good for you."
}
```
This doesn't look like much of an
improvement if our data is a simple
Int, but what if it's more complicated
---
| \*\gGuards
```
def mySteps(t:(String, Int)) =
if(t._1 == "fatbot" && t._2 < 2500) "you aren't even trying"
else if(t._1 == "fatbot" && t._2 < 5000) "that is pitiful"
else if(t._1 == "fatbot" && t._2 < 7500) "ok but still walk more"
else if(t._1 == "fatbot") "hey you walked. good for you."
else "What the heck is a " + t._1 + "?"
```
If we needed to verify the device name as
well, the code gets a little messier.
---
| \*\gGuards
```
def mySteps(t:(String, Int)) = {
val device: String = t._1
if (device == "fatbot") {
val steps: Int = t._2
if (steps < 2500) "you aren't even trying"
else if (steps < 5000) "that is pitiful"
else if (steps < 7500) "ok but still walk more"
else "hey you walked. good for you."
}
else "What the heck is a " + device + "?"
}
```
Or maybe you'd try to get a little fancier with it.
---
| \*\gGuards
```
def mySteps(t:(String, Int)) = t match {
case ("fatbot", s) if (s < 2500) => "you aren't even trying"
case ("fatbot", s) if (s < 5000) => "that is pitiful"
case ("fatbot", s) if (s < 7500) => "ok but still walk more"
case ("fatbot", _) => "hey you walked. good for you."
case (d, _) => "What the heck is an " + d + "?"
}
```
A combination of guards and decomposition make this
look lovely and easy again.
---
| \*\gEnd of Part 2!
/ :monkey_face: :pizza: \s
| :tired_face: Go take a break, you deserve it! :tired_face:
/ :stuck_out_tongue_closed_eyes: :two_hearts: \s