Created
May 29, 2020 07:28
-
-
Save vigneshwaranr/2e5380aba1a7d64ac786746134bb71e3 to your computer and use it in GitHub Desktop.
9e8a12f2112b / MaybeDivideable
This file contains hidden or 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
sealed trait MaybeDivideable { | |
def get: Int //Extraction | |
def flatMap(f: Int => MaybeDivideable): MaybeDivideable | |
} | |
object MaybeDivideable { | |
def apply(value: Int): MaybeDivideable = | |
if (value == 0) UnDivideable else Divideable(value) // Lifting | |
} | |
case class Divideable(value: Int) extends MaybeDivideable { | |
require(value != 0, "value must not be 0") | |
override def get: Int = value | |
override def flatMap(f: Int => MaybeDivideable): MaybeDivideable = f(get) | |
} | |
case object UnDivideable extends MaybeDivideable { | |
override def get: Int = throw new NoSuchFieldException | |
override def flatMap(f: Int => MaybeDivideable): MaybeDivideable = UnDivideable | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment