Last active
August 14, 2021 15:41
-
-
Save zetashift/05f94ebe90ac0ab795eae014181fc74f to your computer and use it in GitHub Desktop.
Options In Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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