Scala: Options Cheat Sheet

OptionDescription
Some(42)Construct a non empty optional value.
NoneThe singleton empty optional value.
Option(null) == None Option(obj.unsafeMethod)
but
Some(null) != None
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 asoption 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 asoption 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 asoption match {
case Some(_) => true
case None => false
}
true if not empty.
option.size
same as
option match {
case Some(_) => 1
case None => 0
}
0 if empty, otherwise 1.
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 Some(_) => false
case None => 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 Some(_) => false
case None => true
}
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.

Leave a Reply

Your email address will not be published. Required fields are marked *