Skip to content

Instantly share code, notes, and snippets.

@vigneshwaranr
Created May 29, 2020 07:28
Show Gist options
  • Save vigneshwaranr/2e5380aba1a7d64ac786746134bb71e3 to your computer and use it in GitHub Desktop.
Save vigneshwaranr/2e5380aba1a7d64ac786746134bb71e3 to your computer and use it in GitHub Desktop.
9e8a12f2112b / MaybeDivideable
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