Skip to content

Commit d4a5c2c

Browse files
committed
Add Option to cheatsheet
1 parent 3646bd3 commit d4a5c2c

File tree

1 file changed

+226
-9
lines changed

1 file changed

+226
-9
lines changed

_overviews/cheatsheets/index.md

Lines changed: 226 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,27 +252,27 @@ breakable {
252252
<tr>
253253
<td><span class="label important">Bad</span><br>
254254
<pre class="highlight"><code>val v42 = 42
255-
Some(3) match {
256-
case Some(v42) => println("42")
257-
case _ => println("Not 42")
255+
3 match {
256+
case v42 => println("42")
257+
case _ => println("Not 42")
258258
}</code></pre></td>
259259
<td>“v42” is interpreted as a name matching any Int value, and “42” is printed.</td>
260260
</tr>
261261
<tr>
262262
<td><span class="label success">Good</span><br>
263263
<pre class="highlight"><code>val v42 = 42
264-
Some(3) match {
265-
case Some(`v42`) => println("42")
266-
case _ => println("Not 42")
264+
3 match {
265+
case `v42` => println("42")
266+
case _ => println("Not 42")
267267
}</code></pre></td>
268268
<td>”`v42`” with backticks is interpreted as the existing val <code class="highlighter-rouge">v42</code>, and “Not 42” is printed.</td>
269269
</tr>
270270
<tr>
271271
<td><span class="label success">Good</span><br>
272272
<pre class="highlight"><code>val UppercaseVal = 42
273-
Some(3) match {
274-
case Some(UppercaseVal) => println("42")
275-
case _ => println("Not 42")
273+
3 match {
274+
case UppercaseVal => println("42")
275+
case _ => println("Not 42")
276276
}</code></pre></td>
277277
<td><code class="highlighter-rouge">UppercaseVal</code> is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within <code class="highlighter-rouge">UppercaseVal</code> is checked against <code class="highlighter-rouge">3</code>, and “Not 42” is printed.</td>
278278
</tr>
@@ -353,6 +353,223 @@ Some(3) match {
353353
<tr>
354354
<td><code class="highlighter-rouge">x: String</code></td>
355355
<td>ascription (compile time)</td>
356+
<td> </td>
357+
</tr>
358+
<tr>
359+
<td><span id="options" class="h2">options</span></td>
360+
<td> </td>
361+
</tr>
362+
<tr>
363+
<td><pre class="highlight"><code>Some(42)</code></pre></td>
364+
<td>Construct a non empty optional value</td>
365+
</tr>
366+
<tr>
367+
<td><pre class="highlight"><code>None</code></pre></td>
368+
<td>The singleton empty optional value</td>
369+
</tr>
370+
<tr>
371+
<td><pre class="highlight"><code>Option(null) == None
372+
Option(obj.unsafeMethod)</code></pre></td>
373+
<td>Null-safe optional value factory</td>
374+
</tr>
375+
<tr>
376+
<td><pre class="highlight"><code>val optStr: Option[String] = None</code></pre>
377+
<em><strong>same as</strong></em>
378+
<pre class="highlight"><code>val optStr = Option.empty[String]</code></pre></td>
379+
<td>Explicit type for empty optional value.<br /> Factory for empty optional value.</td>
380+
</tr>
381+
<tr>
382+
<td><pre class="highlight"><code>val name: Option[String] =
383+
request.getParameter("name")
384+
val upper = name.map {
385+
_.trim
386+
}
387+
.filter {
388+
_.length != 0
389+
}
390+
.map {
391+
_.toUpperCase
392+
}
393+
println(upper.getOrElse(""))
394+
</code></pre></td>
395+
<td>Pipeline style</td>
396+
</tr>
397+
<tr>
398+
<td><pre class="highlight"><code>val upper = for {
399+
name <- request.getParameter("name")
400+
trimmed <- Some(name.trim)
401+
if trimmed.length != 0
402+
upper <- Some(trimmed.toUpperCase)
403+
} yield upper
404+
println(upper.getOrElse(""))</code></pre></td>
405+
<td>for-comprehension syntax</td>
406+
</tr>
407+
<tr>
408+
<td><pre class="highlight"><code>option.map(f(_))</code></pre>
409+
<em><strong>same as</strong></em>
410+
<pre class="highlight"><code>option match {
411+
case Some(x) =&gt; Some(f(x))
412+
case None =&gt; None
413+
}</code></pre></td>
414+
<td>Apply a function on the optional value</td>
415+
</tr>
416+
<tr>
417+
<td><pre class="highlight"><code>option.flatMap(f(_))</code></pre>
418+
<em><strong>same as</strong></em>
419+
<pre class="highlight"><code>option match {
420+
case Some(x) =&gt; f(x)
421+
case None =&gt; None
422+
}</code></pre></td>
423+
<td>Same as map but function must return an optional value</td>
424+
</tr>
425+
<tr>
426+
<td><pre class="highlight"><code>option.foreach(f(_))</code></pre>
427+
<em><strong>same as</strong></em>
428+
<pre class="highlight"><code>option match {
429+
case Some(x) =&gt; f(x)
430+
case None =&gt; ()
431+
}</code></pre></td>
432+
<td>Apply a procedure on optional value</td>
433+
</tr>
434+
<tr>
435+
<td><pre class="highlight"><code>option.fold(y)f(_))</code></pre>
436+
<em><strong>same as</strong></em>
437+
<pre class="highlight"><code>option match {
438+
case Some(x) =&gt; f(x)
439+
case None =&gt; y
440+
}</code></pre></td>
441+
<td>Apply function on optional value, return default if empty</td>
442+
</tr>
443+
<tr>
444+
<td><pre class="highlight"><code>option.collect {
445+
case x =&gt; ...
446+
}</code></pre>
447+
<em><strong>same as</strong></em>
448+
<pre class="highlight"><code>option match {
449+
case Some(x)
450+
if f.isDefinedAt(x) =&gt; ...
451+
case Some(_) =&gt; None
452+
case None =&gt; None
453+
}</code></pre></td>
454+
<td>Apply partial pattern match on optional value</td>
455+
</tr>
456+
<tr>
457+
<td><pre class="highlight"><code>option.isDefined</code></pre>
458+
<em><strong>same as</strong></em>
459+
<pre class="highlight"><code>option match {
460+
case Some(_) =&gt; true
461+
case None =&gt; false
462+
}</code></pre></td>
463+
<td>True if not empty</td>
464+
</tr>
465+
<tr>
466+
<td><pre class="highlight"><code>option.isEmpty</code></pre>
467+
<em><strong>same as</strong></em>
468+
<pre class="highlight"><code>option match {
469+
case Some(_) =&gt; false
470+
case None =&gt; true
471+
}</code></pre></td>
472+
<td>True if empty</td>
473+
</tr>
474+
<tr>
475+
<td><pre class="highlight"><code>option.nonEmpty</code></pre>
476+
<em><strong>same as</strong></em>
477+
<pre class="highlight"><code>option match {
478+
case Some(_) =&gt; true
479+
case None =&gt; false
480+
}</code></pre></td>
481+
<td>True if not empty</td>
482+
</tr>
483+
<tr>
484+
<td><pre class="highlight"><code>option.size</code></pre>
485+
<em><strong>same as</strong></em>
486+
<pre class="highlight"><code>option match {
487+
case Some(_) =&gt; 1
488+
case None =&gt; 0
489+
490+
</code></pre></td>
491+
<td>Zero if empty, otherwise one</td>
492+
</tr>
493+
<tr>
494+
<td><pre class="highlight"><code>option.orElse(Some(y))</code></pre>
495+
<em><strong>same as</strong></em>
496+
<pre class="highlight"><code>option match {
497+
case Some(x) =&gt; Some(x)
498+
case None =&gt; Some(y)
499+
}</code></pre></td>
500+
<td>Evaluate and return alternate optional value if empty</td>
501+
</tr>
502+
<tr>
503+
<td><pre class="highlight"><code>option.getOrElse(y)</code></pre>
504+
<em><strong>same as</strong></em>
505+
<pre class="highlight"><code>option match {
506+
case Some(x) =&gt; x
507+
case None =&gt; y
508+
}</code></pre></td>
509+
<td>Evaluate and return default value if empty</td>
510+
</tr>
511+
<tr>
512+
<td><pre class="highlight"><code>option.get</code></pre>
513+
<em><strong>same as</strong></em>
514+
<pre class="highlight"><code>option match {
515+
case Some(x) =&gt; x
516+
case None =&gt; throw new Exception
517+
}</code></pre></td>
518+
<td>Return value, throw exception if empty</td>
519+
</tr>
520+
<tr>
521+
<td><pre class="highlight"><code>option.orNull</code></pre>
522+
<em><strong>same as</strong></em>
523+
<pre class="highlight"><code>option match {
524+
case Some(x) =&gt; x
525+
case None =&gt; null
526+
}</code></pre></td>
527+
<td>Return value, null if empty</td>
528+
</tr>
529+
<tr>
530+
<td><pre class="highlight"><code>option.filter(f(_))</code></pre>
531+
<em><strong>same as</strong></em>
532+
<pre class="highlight"><code>option match {
533+
case Some(x) if f(x) =&gt; Some(x)
534+
case _ =&gt; None
535+
}</code></pre></td>
536+
<td>Optional value satisfies predicate</td>
537+
</tr>
538+
<tr>
539+
<td><pre class="highlight"><code>option.filterNot(f(_))</code></pre>
540+
<em><strong>same as</strong></em>
541+
<pre class="highlight"><code>option match {
542+
case Some(x) if !f(x) =&gt; Some(x)
543+
case _ =&gt; None
544+
}</code></pre></td>
545+
<td>Optional value doesn't satisfy predicate</td>
546+
</tr>
547+
<tr>
548+
<td><pre class="highlight"><code>option.exists(f(_))</code></pre>
549+
<em><strong>same as</strong></em>
550+
<pre class="highlight"><code>option match {
551+
case Some(x) if (f(x)) =&gt; true
552+
case _ =&gt; false
553+
}</code></pre></td>
554+
<td>Apply predicate on optional value or false if empty</td>
555+
</tr>
556+
<tr>
557+
<td><pre class="highlight"><code>option.forall(f(_))</code></pre>
558+
<em><strong>same as</strong></em>
559+
<pre class="highlight"><code>option match {
560+
case Some(x) if f(x) =&gt; true
561+
case None =&gt; false
562+
}</code></pre></td>
563+
<td>Apply predicate on optional value or true if empty</td>
564+
</tr>
565+
<tr>
566+
<td><pre class="highlight"><code>option.contains(y)</code></pre>
567+
<em><strong>same as</strong></em>
568+
<pre class="highlight"><code>option match {
569+
case Some(x) =&gt; x == y
570+
case None =&gt; false
571+
}</code></pre></td>
572+
<td>Checks if value equals optional value or false if empty</td>
356573
</tr>
357574
</tbody>
358575
</table>

0 commit comments

Comments
 (0)