@@ -26,16 +26,16 @@ permalink: /cheatsheets/index.html
26
26
<td> </td>
27
27
</tr>
28
28
<tr>
29
- <td><pre class="highlight"><code>var x = 5</code></pre><br /> <span class="label success">Good</span><br><pre class="highlight"><code>x= 6</code></pre></td>
30
- <td>variable</td>
29
+ <td><pre class="highlight"><code>var x = 5</code></pre><br /> <span class="label success">Good</span><br><pre class="highlight"><code>x = 6</code></pre></td>
30
+ <td>variable. </td>
31
31
</tr>
32
32
<tr>
33
- <td><pre class="highlight"><code>val x = 5</code></pre><br /> <span class="label important">Bad</span><br><pre class="highlight"><code>x= 6</code></pre></td>
34
- <td>constant</td>
33
+ <td><pre class="highlight"><code>val x = 5</code></pre><br /> <span class="label important">Bad</span><br><pre class="highlight"><code>x = 6</code></pre></td>
34
+ <td>constant. </td>
35
35
</tr>
36
36
<tr>
37
37
<td><pre class="highlight"><code>var x: Double = 5</code></pre></td>
38
- <td>explicit type</td>
38
+ <td>explicit type. </td>
39
39
</tr>
40
40
<tr>
41
41
<td><span id="functions" class="h2">functions</span></td>
@@ -58,15 +58,15 @@ permalink: /cheatsheets/index.html
58
58
<td>call-by-value <br /> call-by-name (lazy parameters)</td>
59
59
</tr>
60
60
<tr>
61
- <td><pre class="highlight"><code>(x:R) => x * x</code></pre></td>
61
+ <td><pre class="highlight"><code>(x: R) => x * x</code></pre></td>
62
62
<td>anonymous function</td>
63
63
</tr>
64
64
<tr>
65
- <td><pre class="highlight"><code>(1 to 5).map(_ * 2)</code></pre> vs.<br /> <pre class="highlight"><code>(1 to 5).reduceLeft( _ + _ )</code></pre></td>
65
+ <td><pre class="highlight"><code>(1 to 5).map(_ * 2)</code></pre> vs.<br /> <pre class="highlight"><code>(1 to 5).reduceLeft(_ + _)</code></pre></td>
66
66
<td>anonymous function: underscore is positionally matched arg.</td>
67
67
</tr>
68
68
<tr>
69
- <td><pre class="highlight"><code>(1 to 5).map( x => x * x )</code></pre></td>
69
+ <td><pre class="highlight"><code>(1 to 5).map(x => x * x)</code></pre></td>
70
70
<td>anonymous function: to use an arg twice, have to name it.</td>
71
71
</tr>
72
72
<tr>
@@ -95,15 +95,15 @@ permalink: /cheatsheets/index.html
95
95
<td >anonymous functions: to pass in multiple blocks, need outer parens.</td >
96
96
</tr >
97
97
<tr >
98
- <td ><pre class =" highlight " ><code >val zscore =
98
+ <td ><pre class =" highlight " ><code >val zscore =
99
99
(mean: R, sd: R) => ;
100
- (x: R ) => ;
100
+ (x: R ) => ;
101
101
(x - mean) / sd</code ></pre ></td >
102
102
<td >currying, obvious syntax.</td >
103
103
</tr >
104
104
<tr >
105
105
<td ><pre class =" highlight " ><code >def zscore(mean: R , sd: R ) =
106
- (x: R ) => ;
106
+ (x: R ) => ;
107
107
(x - mean) / sd</code ></pre ></td >
108
108
<td >currying, obvious syntax</td >
109
109
</tr >
@@ -127,7 +127,7 @@ permalink: /cheatsheets/index.html
127
127
<td >infix sugar.</td >
128
128
</tr >
129
129
<tr >
130
- <td ><pre class =" highlight " ><code >def sum(args: Int* ) =
130
+ <td ><pre class =" highlight " ><code >def sum(args: Int* ) =
131
131
args.reduceLeft(_ +_ )</code ></pre ></td >
132
132
<td >varargs.</td >
133
133
</tr >
@@ -210,7 +210,7 @@ permalink: /cheatsheets/index.html
210
210
<td >conditional sugar.</td >
211
211
</tr >
212
212
<tr >
213
- <td ><pre class =" highlight " ><code >while (x < ; 5) {
213
+ <td ><pre class =" highlight " ><code >while (x < ; 5) {
214
214
println(x)
215
215
x += 1
216
216
}</code ></pre ></td >
@@ -225,67 +225,68 @@ permalink: /cheatsheets/index.html
225
225
</tr >
226
226
<tr >
227
227
<td ><pre class =" highlight " ><code >import scala.util.control.Breaks._
228
+
228
229
breakable {
229
- for (x < - xs) {
230
- if (Math.random < 0.1)
230
+ for (x & lt ; - xs) {
231
+ if (Math.random & lt ; 0.1)
231
232
break
232
233
}
233
234
}</code ></pre ></td >
234
235
<td >break. (<a href =" http://www.slideshare.net/Odersky/fosdem-2009-1013261/21 " >slides</a >)</td >
235
236
</tr >
236
237
<tr >
237
- <td ><pre class =" highlight " ><code >for (x < ; - xs if x% 2 == 0)
238
- yield x * 10</code ></pre >
238
+ <td ><pre class =" highlight " ><code >for (x < ; - xs if x % 2 == 0)
239
+ yield x * 10</code ></pre >
239
240
<br ><em ><strong >same as</strong ></em ><br >
240
- <pre class =" highlight " ><code >xs.filter(_ % 2 == 0).map( _ * 10)</code ></pre ></td >
241
- <td >for comprehension: filter/map</td >
241
+ <pre class =" highlight " ><code >xs.filter(_ % 2 == 0).map(_ * 10)</code ></pre ></td >
242
+ <td >for comprehension: filter/map. </td >
242
243
</tr >
243
244
<tr >
244
245
<td ><pre class =" highlight " ><code >for ((x, y) < ; - xs zip ys)
245
- yield x * y</code ></pre >
246
+ yield x * y</code ></pre >
246
247
<br ><em ><strong >same as</strong ></em ><br >
247
248
<pre class =" highlight " ><code >(xs zip ys) map {
248
249
case (x, y) => ; x * y
249
250
}</code ></pre ></td >
250
- <td >for comprehension: destructuring bind</td >
251
+ <td >for comprehension: destructuring bind. </td >
251
252
</tr >
252
253
<tr >
253
254
<td ><pre class =" highlight " ><code >for (x < ; - xs; y < ; - ys)
254
- yield x * y</code ></pre >
255
+ yield x * y</code ></pre >
255
256
<br ><em ><strong >same as</strong ></em ><br >
256
257
<pre class =" highlight " ><code >xs flatMap { x => ;
257
258
ys map { y => ;
258
259
x * y
259
260
}
260
261
}</code ></pre ></td >
261
- <td >for comprehension: cross product</td >
262
+ <td >for comprehension: cross product. </td >
262
263
</tr >
263
264
<tr >
264
- <td ><pre class =" highlight " ><code >for (x < - xs; y < - ys) {
265
+ <td ><pre class =" highlight " ><code >for (x & lt ; - xs; y & lt ; - ys) {
265
266
val div = x / y.toFloat
266
267
println("%d/%d = %.1f".format(x, y, div))
267
268
}</code ></pre ></td >
268
- <td >for comprehension: imperative-ish<br /><a href =" http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax " >sprintf-style</a ></td >
269
+ <td >for comprehension: imperative-ish. <br /><a href =" http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax " >sprintf-style</a >. </td >
269
270
</tr >
270
271
<tr >
271
- <td ><pre class =" highlight " ><code >for (i < - 1 to 5) {
272
+ <td ><pre class =" highlight " ><code >for (i & lt ; - 1 to 5) {
272
273
println(i)
273
274
}</code ></pre ></td >
274
- <td >for comprehension: iterate including the upper bound</td >
275
+ <td >for comprehension: iterate including the upper bound. </td >
275
276
</tr >
276
277
<tr >
277
- <td ><pre class =" highlight " ><code >for (i < - 1 until 5) {
278
+ <td ><pre class =" highlight " ><code >for (i & lt ; - 1 until 5) {
278
279
println(i)
279
280
}</code ></pre ></td >
280
- <td >for comprehension: iterate omitting the upper bound</td >
281
+ <td >for comprehension: iterate omitting the upper bound. </td >
281
282
</tr >
282
283
<tr >
283
284
<td ><span id =" pattern_matching " class =" h2 " >pattern matching</span ></td >
284
285
<td > </td >
285
286
</tr >
286
287
<tr >
287
288
<td ><span class =" label success " >Good</span ><br > <pre class =" highlight " ><code >(xs zip ys) map {
288
- case (x, y) => ; x * y
289
+ case (x, y) => ; x * y
289
290
}</code ></pre ><br /> <span class =" label important " >Bad</span ><br > <pre class =" highlight " ><code >(xs zip ys) map {
290
291
(x, y) => ; x * y
291
292
}</code ></pre ></td >
@@ -307,7 +308,7 @@ yield x * y</code></pre>
307
308
case ` v42 ` => ; println("42")
308
309
case _ => ; println("Not 42")
309
310
}</code ></pre ></td >
310
- <td >”` v42 ` ” with backticks is interpreted as the existing val <pre class = " highlight " >< code >v42</code ></ pre >, and “Not 42” is printed.</td >
311
+ <td >”` v42 ` ” with backticks is interpreted as the existing val <code >v42</code >, and “Not 42” is printed.</td >
311
312
</tr >
312
313
<tr >
313
314
<td ><span class =" label success " >Good</span ><br >
@@ -380,23 +381,23 @@ yield x * y</code></pre>
380
381
</tr >
381
382
<tr >
382
383
<td ><span class =" label important " >Bad</span ><br > <pre class =" highlight " ><code >new List[ Int] </code ></pre ><br /> <span class =" label success " >Good</span ><br > <pre class =" highlight " ><code >List(1, 2, 3)</code ></pre ></td >
383
- <td >type error: abstract type<br />instead, convention: callable factory shadowing the type</td >
384
+ <td >type error: abstract type<br />instead, convention: callable factory shadowing the type. </td >
384
385
</tr >
385
386
<tr >
386
387
<td ><pre class =" highlight " ><code >classOf[ String] </code ></pre ></td >
387
388
<td >class literal.</td >
388
389
</tr >
389
390
<tr >
390
391
<td ><pre class =" highlight " ><code >x.isInstanceOf[ String] </code ></pre ></td >
391
- <td >type check (runtime)</td >
392
+ <td >type check (runtime). </td >
392
393
</tr >
393
394
<tr >
394
395
<td ><pre class =" highlight " ><code >x.asInstanceOf[ String] </code ></pre ></td >
395
- <td >type cast (runtime)</td >
396
+ <td >type cast (runtime). </td >
396
397
</tr >
397
398
<tr >
398
399
<td ><pre class =" highlight " ><code >x: String</code ></pre ></td >
399
- <td >ascription (compile time)</td >
400
+ <td >ascription (compile time). </td >
400
401
<td > </td >
401
402
</tr >
402
403
<tr >
@@ -405,16 +406,16 @@ yield x * y</code></pre>
405
406
</tr >
406
407
<tr >
407
408
<td ><pre class =" highlight " ><code >Some(42)</code ></pre ></td >
408
- <td >Construct a non empty optional value</td >
409
+ <td >Construct a non empty optional value. </td >
409
410
</tr >
410
411
<tr >
411
412
<td ><pre class =" highlight " ><code >None</code ></pre ></td >
412
- <td >The singleton empty optional value</td >
413
+ <td >The singleton empty optional value. </td >
413
414
</tr >
414
415
<tr >
415
416
<td ><pre class =" highlight " ><code >Option(null) == None
416
417
Option(obj.unsafeMethod)</code ></pre ></td >
417
- <td >Null-safe optional value factory</td >
418
+ <td >Null-safe optional value factory. </td >
418
419
</tr >
419
420
<tr >
420
421
<td ><pre class =" highlight " ><code >val optStr: Option[ String] = None</code ></pre >
@@ -427,26 +428,23 @@ Option(obj.unsafeMethod)</code></pre></td>
427
428
request.getParameter("name")
428
429
val upper = name.map {
429
430
_ .trim
430
- }
431
- .filter {
431
+ } filter {
432
432
_ .length != 0
433
- }
434
- .map {
433
+ } map {
435
434
_ .toUpperCase
436
435
}
437
- println(upper.getOrElse(""))
438
- </code ></pre ></td >
439
- <td >Pipeline style</td >
436
+ println(upper.getOrElse(""))</code ></pre ></td >
437
+ <td >Pipeline style.</td >
440
438
</tr >
441
439
<tr >
442
440
<td ><pre class =" highlight " ><code >val upper = for {
443
- name < - request.getParameter("name")
444
- trimmed < - Some(name.trim)
441
+ name & lt ; - request.getParameter("name")
442
+ trimmed & lt ; - Some(name.trim)
445
443
if trimmed.length != 0
446
- upper < - Some(trimmed.toUpperCase)
444
+ upper & lt ; - Some(trimmed.toUpperCase)
447
445
} yield upper
448
446
println(upper.getOrElse(""))</code ></pre ></td >
449
- <td >for-comprehension syntax</td >
447
+ <td >for-comprehension syntax. </td >
450
448
</tr >
451
449
<tr >
452
450
<td ><pre class =" highlight " ><code >option.map(f(_ ))</code ></pre >
@@ -455,7 +453,7 @@ println(upper.getOrElse(""))</code></pre></td>
455
453
case Some(x) => ; Some(f(x))
456
454
case None => ; None
457
455
}</code ></pre ></td >
458
- <td >Apply a function on the optional value</td >
456
+ <td >Apply a function on the optional value. </td >
459
457
</tr >
460
458
<tr >
461
459
<td ><pre class =" highlight " ><code >option.flatMap(f(_ ))</code ></pre >
@@ -464,7 +462,7 @@ println(upper.getOrElse(""))</code></pre></td>
464
462
case Some(x) => ; f(x)
465
463
case None => ; None
466
464
}</code ></pre ></td >
467
- <td >Same as map but function must return an optional value</td >
465
+ <td >Same as map but function must return an optional value. </td >
468
466
</tr >
469
467
<tr >
470
468
<td ><pre class =" highlight " ><code >optionOfOption.flatten</code ></pre >
@@ -473,7 +471,7 @@ println(upper.getOrElse(""))</code></pre></td>
473
471
case Some(Some(x)) => ; Some(x)
474
472
case _ => ; None
475
473
}</code ></pre ></td >
476
- <td >Extract nested option</td >
474
+ <td >Extract nested option. </td >
477
475
</tr >
478
476
<tr >
479
477
<td ><pre class =" highlight " ><code >option.foreach(f(_ ))</code ></pre >
@@ -491,7 +489,7 @@ println(upper.getOrElse(""))</code></pre></td>
491
489
case Some(x) => ; f(x)
492
490
case None => ; y
493
491
}</code ></pre ></td >
494
- <td >Apply function on optional value, return default if empty</td >
492
+ <td >Apply function on optional value, return default if empty. </td >
495
493
</tr >
496
494
<tr >
497
495
<td ><pre class =" highlight " ><code >option.collect {
0 commit comments