Skip to content

Instantly share code, notes, and snippets.

@zetashift
Last active August 14, 2021 15:41
Show Gist options
  • Save zetashift/05f94ebe90ac0ab795eae014181fc74f to your computer and use it in GitHub Desktop.
Save zetashift/05f94ebe90ac0ab795eae014181fc74f to your computer and use it in GitHub Desktop.
Options In Scala
enum Option[+A]:
case Some(value: A)
case None
def map[B](f: A => B): Option[B] = this match
case None => None
case Some(value) => Some(f(value))
def flatMap[B](f: A => Option[B]): Option[B] = this match
case None => None
case Some(value) => f(value)
def isSome: Boolean = this match
case None => false
case _ => true
def isNone: Boolean = this match
case None => true
case _ => false
def getOrElse[B <: A](otherwise: B): B = this match
case None => otherwise
case Some(value) => value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment