Skip to content

Commit 2cbf40a

Browse files
committed
whitespace, dots, formatting
1 parent ae56b48 commit 2cbf40a

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

_overviews/cheatsheets/index.md

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ permalink: /cheatsheets/index.html
2626
<td> </td>
2727
</tr>
2828
<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>
3131
</tr>
3232
<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>
3535
</tr>
3636
<tr>
3737
<td><pre class="highlight"><code>var x: Double = 5</code></pre></td>
38-
<td>explicit type</td>
38+
<td>explicit type.</td>
3939
</tr>
4040
<tr>
4141
<td><span id="functions" class="h2">functions</span></td>
@@ -58,15 +58,15 @@ permalink: /cheatsheets/index.html
5858
<td>call-by-value <br /> call-by-name (lazy parameters)</td>
5959
</tr>
6060
<tr>
61-
<td><pre class="highlight"><code>(x:R) =&gt; x * x</code></pre></td>
61+
<td><pre class="highlight"><code>(x: R) =&gt; x * x</code></pre></td>
6262
<td>anonymous function</td>
6363
</tr>
6464
<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>
6666
<td>anonymous function: underscore is positionally matched arg.</td>
6767
</tr>
6868
<tr>
69-
<td><pre class="highlight"><code>(1 to 5).map( x =&gt; x * x )</code></pre></td>
69+
<td><pre class="highlight"><code>(1 to 5).map(x =&gt; x * x)</code></pre></td>
7070
<td>anonymous function: to use an arg twice, have to name it.</td>
7171
</tr>
7272
<tr>
@@ -95,15 +95,15 @@ permalink: /cheatsheets/index.html
9595
<td>anonymous functions: to pass in multiple blocks, need outer parens.</td>
9696
</tr>
9797
<tr>
98-
<td><pre class="highlight"><code>val zscore =
98+
<td><pre class="highlight"><code>val zscore =
9999
(mean: R, sd: R) =&gt;
100-
(x:R) =&gt;
100+
(x:R) =&gt;
101101
(x - mean) / sd</code></pre></td>
102102
<td>currying, obvious syntax.</td>
103103
</tr>
104104
<tr>
105105
<td><pre class="highlight"><code>def zscore(mean:R, sd:R) =
106-
(x:R) =&gt;
106+
(x:R) =&gt;
107107
(x - mean) / sd</code></pre></td>
108108
<td>currying, obvious syntax</td>
109109
</tr>
@@ -127,7 +127,7 @@ permalink: /cheatsheets/index.html
127127
<td>infix sugar.</td>
128128
</tr>
129129
<tr>
130-
<td><pre class="highlight"><code>def sum(args: Int*) =
130+
<td><pre class="highlight"><code>def sum(args: Int*) =
131131
args.reduceLeft(_+_)</code></pre></td>
132132
<td>varargs.</td>
133133
</tr>
@@ -210,7 +210,7 @@ permalink: /cheatsheets/index.html
210210
<td>conditional sugar.</td>
211211
</tr>
212212
<tr>
213-
<td><pre class="highlight"><code>while (x &lt; 5) {
213+
<td><pre class="highlight"><code>while (x &lt; 5) {
214214
println(x)
215215
x += 1
216216
}</code></pre></td>
@@ -225,67 +225,68 @@ permalink: /cheatsheets/index.html
225225
</tr>
226226
<tr>
227227
<td><pre class="highlight"><code>import scala.util.control.Breaks._
228+
228229
breakable {
229-
for (x <- xs) {
230-
if (Math.random < 0.1)
230+
for (x &lt;- xs) {
231+
if (Math.random &lt; 0.1)
231232
break
232233
}
233234
}</code></pre></td>
234235
<td>break. (<a href="http://www.slideshare.net/Odersky/fosdem-2009-1013261/21">slides</a>)</td>
235236
</tr>
236237
<tr>
237-
<td><pre class="highlight"><code>for (x &lt;- xs if x%2 == 0)
238-
yield x * 10</code></pre>
238+
<td><pre class="highlight"><code>for (x &lt;- xs if x % 2 == 0)
239+
yield x * 10</code></pre>
239240
<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>
242243
</tr>
243244
<tr>
244245
<td><pre class="highlight"><code>for ((x, y) &lt;- xs zip ys)
245-
yield x * y</code></pre>
246+
yield x * y</code></pre>
246247
<br><em><strong>same as</strong></em><br>
247248
<pre class="highlight"><code>(xs zip ys) map {
248249
case (x, y) =&gt; x * y
249250
}</code></pre></td>
250-
<td>for comprehension: destructuring bind</td>
251+
<td>for comprehension: destructuring bind.</td>
251252
</tr>
252253
<tr>
253254
<td><pre class="highlight"><code>for (x &lt;- xs; y &lt;- ys)
254-
yield x * y</code></pre>
255+
yield x * y</code></pre>
255256
<br><em><strong>same as</strong></em><br>
256257
<pre class="highlight"><code>xs flatMap { x =&gt;
257258
ys map { y =&gt;
258259
x * y
259260
}
260261
}</code></pre></td>
261-
<td>for comprehension: cross product</td>
262+
<td>for comprehension: cross product.</td>
262263
</tr>
263264
<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) {
265266
val div = x / y.toFloat
266267
println("%d/%d = %.1f".format(x, y, div))
267268
}</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>
269270
</tr>
270271
<tr>
271-
<td><pre class="highlight"><code>for (i <- 1 to 5) {
272+
<td><pre class="highlight"><code>for (i &lt;- 1 to 5) {
272273
println(i)
273274
}</code></pre></td>
274-
<td>for comprehension: iterate including the upper bound</td>
275+
<td>for comprehension: iterate including the upper bound.</td>
275276
</tr>
276277
<tr>
277-
<td><pre class="highlight"><code>for (i <- 1 until 5) {
278+
<td><pre class="highlight"><code>for (i &lt;- 1 until 5) {
278279
println(i)
279280
}</code></pre></td>
280-
<td>for comprehension: iterate omitting the upper bound</td>
281+
<td>for comprehension: iterate omitting the upper bound.</td>
281282
</tr>
282283
<tr>
283284
<td><span id="pattern_matching" class="h2">pattern matching</span></td>
284285
<td> </td>
285286
</tr>
286287
<tr>
287288
<td><span class="label success">Good</span><br> <pre class="highlight"><code>(xs zip ys) map {
288-
case (x, y) =&gt; x * y
289+
case (x, y) =&gt; x * y
289290
}</code></pre><br /> <span class="label important">Bad</span><br> <pre class="highlight"><code>(xs zip ys) map {
290291
(x, y) =&gt; x * y
291292
}</code></pre></td>
@@ -307,7 +308,7 @@ yield x * y</code></pre>
307308
case `v42` =&gt; println("42")
308309
case _ =&gt; println("Not 42")
309310
}</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>
311312
</tr>
312313
<tr>
313314
<td><span class="label success">Good</span><br>
@@ -380,23 +381,23 @@ yield x * y</code></pre>
380381
</tr>
381382
<tr>
382383
<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>
384385
</tr>
385386
<tr>
386387
<td><pre class="highlight"><code>classOf[String]</code></pre></td>
387388
<td>class literal.</td>
388389
</tr>
389390
<tr>
390391
<td><pre class="highlight"><code>x.isInstanceOf[String]</code></pre></td>
391-
<td>type check (runtime)</td>
392+
<td>type check (runtime).</td>
392393
</tr>
393394
<tr>
394395
<td><pre class="highlight"><code>x.asInstanceOf[String]</code></pre></td>
395-
<td>type cast (runtime)</td>
396+
<td>type cast (runtime).</td>
396397
</tr>
397398
<tr>
398399
<td><pre class="highlight"><code>x: String</code></pre></td>
399-
<td>ascription (compile time)</td>
400+
<td>ascription (compile time).</td>
400401
<td> </td>
401402
</tr>
402403
<tr>
@@ -405,16 +406,16 @@ yield x * y</code></pre>
405406
</tr>
406407
<tr>
407408
<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>
409410
</tr>
410411
<tr>
411412
<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>
413414
</tr>
414415
<tr>
415416
<td><pre class="highlight"><code>Option(null) == None
416417
Option(obj.unsafeMethod)</code></pre></td>
417-
<td>Null-safe optional value factory</td>
418+
<td>Null-safe optional value factory.</td>
418419
</tr>
419420
<tr>
420421
<td><pre class="highlight"><code>val optStr: Option[String] = None</code></pre>
@@ -427,26 +428,23 @@ Option(obj.unsafeMethod)</code></pre></td>
427428
request.getParameter("name")
428429
val upper = name.map {
429430
_.trim
430-
}
431-
.filter {
431+
} filter {
432432
_.length != 0
433-
}
434-
.map {
433+
} map {
435434
_.toUpperCase
436435
}
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>
440438
</tr>
441439
<tr>
442440
<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)
445443
if trimmed.length != 0
446-
upper <- Some(trimmed.toUpperCase)
444+
upper &lt;- Some(trimmed.toUpperCase)
447445
} yield upper
448446
println(upper.getOrElse(""))</code></pre></td>
449-
<td>for-comprehension syntax</td>
447+
<td>for-comprehension syntax.</td>
450448
</tr>
451449
<tr>
452450
<td><pre class="highlight"><code>option.map(f(_))</code></pre>
@@ -455,7 +453,7 @@ println(upper.getOrElse(""))</code></pre></td>
455453
case Some(x) =&gt; Some(f(x))
456454
case None =&gt; None
457455
}</code></pre></td>
458-
<td>Apply a function on the optional value</td>
456+
<td>Apply a function on the optional value.</td>
459457
</tr>
460458
<tr>
461459
<td><pre class="highlight"><code>option.flatMap(f(_))</code></pre>
@@ -464,7 +462,7 @@ println(upper.getOrElse(""))</code></pre></td>
464462
case Some(x) =&gt; f(x)
465463
case None =&gt; None
466464
}</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>
468466
</tr>
469467
<tr>
470468
<td><pre class="highlight"><code>optionOfOption.flatten</code></pre>
@@ -473,7 +471,7 @@ println(upper.getOrElse(""))</code></pre></td>
473471
case Some(Some(x)) =&gt; Some(x)
474472
case _ =&gt; None
475473
}</code></pre></td>
476-
<td>Extract nested option</td>
474+
<td>Extract nested option.</td>
477475
</tr>
478476
<tr>
479477
<td><pre class="highlight"><code>option.foreach(f(_))</code></pre>
@@ -491,7 +489,7 @@ println(upper.getOrElse(""))</code></pre></td>
491489
case Some(x) =&gt; f(x)
492490
case None =&gt; y
493491
}</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>
495493
</tr>
496494
<tr>
497495
<td><pre class="highlight"><code>option.collect {

0 commit comments

Comments
 (0)