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
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")
}
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
, and “Not 42” is printed.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.x: String
Some(42)
None
Option(null) == None
+Option(obj.unsafeMethod)
val optStr: Option[String] = None
+ same as
+ val optStr = Option.empty[String]
val name: Option[String] =
+ request.getParameter("name")
+val upper = name.map {
+ _.trim
+}
+.filter {
+ _.length != 0
+}
+.map {
+ _.toUpperCase
+}
+println(upper.getOrElse(""))
+
val upper = for {
+ name <- request.getParameter("name")
+ trimmed <- Some(name.trim)
+ if trimmed.length != 0
+ upper <- Some(trimmed.toUpperCase)
+} yield upper
+println(upper.getOrElse(""))
option.map(f(_))
+ same as
+ option match {
+ case Some(x) => Some(f(x))
+ case None => None
+}
option.flatMap(f(_))
+ same as
+ option match {
+ case Some(x) => f(x)
+ case None => None
+}
optionOfOption.flatten
+ same as
+ optionOfOption match {
+ case Some(Some(x)) => Some(x)
+ case _ => None
+}
option.foreach(f(_))
+ same as
+ option match {
+ case Some(x) => f(x)
+ case None => ()
+}
option.fold(y)(f(_))
+ same as
+ option match {
+ case Some(x) => f(x)
+ case None => y
+}
option.collect {
+ case x => ...
+}
+ same as
+ option match {
+ case Some(x)
+ if f.isDefinedAt(x) => ...
+ case Some(_) => None
+ case None => None
+}
option.isDefined
+ same as
+ option match {
+ case Some(_) => true
+ case None => false
+}
option.isEmpty
+ same as
+ option match {
+ case Some(_) => false
+ case None => true
+}
option.nonEmpty
+ same as
+ option match {
+ case Some(_) => true
+ case None => false
+}
option.size
+ same as
+ option match {
+ case Some(_) => 1
+ case None => 0
+}
option.orElse(Some(y))
+ same as
+ option match {
+ case Some(x) => Some(x)
+ case None => Some(y)
+}
option.getOrElse(y)
+ same as
+ option match {
+ case Some(x) => x
+ case None => y
+}
option.get
+ same as
+ option match {
+ case Some(x) => x
+ case None => throw new Exception
+}
option.orNull
+ same as
+ option match {
+ case Some(x) => x
+ case None => null
+}
option.filter(f)
+ same as
+ option match {
+ case Some(x) if f(x) => Some(x)
+ case _ => None
+}
option.filterNot(f(_))
+ same as
+ option match {
+ case Some(x) if !f(x) => Some(x)
+ case _ => None
+}
option.exists(f(_))
+ same as
+ option match {
+ case Some(x) if f(x) => true
+ case _ => false
+}
option.forall(f(_))
+ same as
+ option match {
+ case Some(x) if f(x) => true
+ case None => false
+}
option.contains(y)
+ same as
+ option match {
+ case Some(x) => x == y
+ case None => false
+}