From f09c27368bd16d4ad3b59922cb34231aece6ebf9 Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Wed, 6 Feb 2019 22:26:51 -0500 Subject: [PATCH] Add Option to cheatsheet --- _overviews/cheatsheets/index.md | 243 ++++++++++++++++++++++++++++++-- 1 file changed, 234 insertions(+), 9 deletions(-) diff --git a/_overviews/cheatsheets/index.md b/_overviews/cheatsheets/index.md index 6ccaad378d..42c2b9c06b 100644 --- a/_overviews/cheatsheets/index.md +++ b/_overviews/cheatsheets/index.md @@ -294,27 +294,27 @@ yield x * y Bad
val v42 = 42
-Some(3) match {
-  case Some(v42) => println("42")
-  case _ => println("Not 42")
+3 match {
+  case v42 => println("42")
+  case _   => println("Not 42")
 }
“v42” is interpreted as a name matching any Int value, and “42” is printed. Good
val v42 = 42
-Some(3) match {
-  case Some(`v42`) => println("42")
-  case _ => println("Not 42")
+3 match {
+  case `v42` => println("42")
+  case _     => println("Not 42")
 }
”`v42`” with backticks is interpreted as the existing val
v42
, and “Not 42” is printed. Good
val UppercaseVal = 42
-Some(3) match {
-  case Some(UppercaseVal) => println("42")
-  case _ => println("Not 42")
+3 match {
+  case UppercaseVal => println("42")
+  case _            => println("Not 42")
 }
UppercaseVal
is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within
UppercaseVal
is checked against
3
, and “Not 42” is printed. @@ -397,6 +397,231 @@ Some(3) match {
x: String
ascription (compile time) + + + + options + + + +
Some(42)
+ Construct a non empty optional value + + +
None
+ The singleton empty optional value + + +
Option(null) == None
+Option(obj.unsafeMethod)
+ Null-safe optional value factory + + +
val optStr: Option[String] = None
+ same as +
val optStr = Option.empty[String]
+ Explicit type for empty optional value.
Factory for empty optional value. + + +
val name: Option[String] =
+  request.getParameter("name")
+val upper = name.map {
+  _.trim
+}
+.filter {
+  _.length != 0
+}
+.map {
+  _.toUpperCase
+}
+println(upper.getOrElse(""))
+
+ Pipeline style + + +
val upper = for {
+  name <- request.getParameter("name")
+  trimmed <- Some(name.trim)
+    if trimmed.length != 0
+  upper <- Some(trimmed.toUpperCase)
+} yield upper
+println(upper.getOrElse(""))
+ for-comprehension syntax + + +
option.map(f(_))
+ same as +
option match {
+  case Some(x) => Some(f(x))
+  case None    => None
+}
+ Apply a function on the optional value + + +
option.flatMap(f(_))
+ same as +
option match {
+  case Some(x) => f(x)
+  case None    => None
+}
+ Same as map but function must return an optional value + + +
optionOfOption.flatten
+ same as +
optionOfOption match {
+  case Some(Some(x)) => Some(x)
+  case _             => None
+}
+ Extract nested option + + +
option.foreach(f(_))
+ same as +
option match {
+  case Some(x) => f(x)
+  case None    => ()
+}
+ Apply a procedure on optional value + + +
option.fold(y)(f(_))
+ same as +
option match {
+  case Some(x) => f(x)
+  case None    => y
+}
+ Apply function on optional value, return default if empty + + +
option.collect {
+  case x => ...
+}
+ same as +
option match {
+  case Some(x)
+    if f.isDefinedAt(x) => ...
+  case Some(_)          => None
+  case None             => None
+}
+ Apply partial pattern match on optional value + + +
option.isDefined
+ same as +
option match {
+  case Some(_) => true
+  case None    => false
+}
+ True if not empty + + +
option.isEmpty
+ same as +
option match {
+  case Some(_) => false
+  case None    => true
+}
+ True if empty + + +
option.nonEmpty
+ same as +
option match {
+  case Some(_) => true
+  case None    => false
+}
+ True if not empty + + +
option.size
+ same as +
option match {
+  case Some(_) => 1
+  case None    => 0
+}
+ Zero if empty, otherwise one + + +
option.orElse(Some(y))
+ same as +
option match {
+  case Some(x) => Some(x)
+  case None    => Some(y)
+}
+ Evaluate and return alternate optional value if empty + + +
option.getOrElse(y)
+ same as +
option match {
+  case Some(x) => x
+  case None    => y
+}
+ Evaluate and return default value if empty + + +
option.get
+ same as +
option match {
+  case Some(x) => x
+  case None    => throw new Exception
+}
+ Return value, throw exception if empty + + +
option.orNull
+ same as +
option match {
+  case Some(x) => x
+  case None    => null
+}
+ Return value, null if empty + + +
option.filter(f)
+ same as +
option match {
+  case Some(x) if f(x) => Some(x)
+  case _               => None
+}
+ Optional value satisfies predicate + + +
option.filterNot(f(_))
+ same as +
option match {
+  case Some(x) if !f(x) => Some(x)
+  case _                => None
+}
+ Optional value doesn't satisfy predicate + + +
option.exists(f(_))
+ same as +
option match {
+  case Some(x) if f(x) => true
+  case _               => false
+}
+ Apply predicate on optional value or false if empty + + +
option.forall(f(_))
+ same as +
option match {
+  case Some(x) if f(x) => true
+  case None            => false
+}
+ Apply predicate on optional value or true if empty + + +
option.contains(y)
+ same as +
option match {
+  case Some(x) => x == y
+  case None    => false
+}
+ Checks if value equals optional value or false if empty