Last active
January 11, 2017 16:01
-
-
Save chrislewis/9e8b2e1e99dc6ca5d80c81a679e84e43 to your computer and use it in GitHub Desktop.
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
/** A boxed union of 3 types; same principle applies for > 3. */ | |
sealed trait OneOf3[A, B, C] { | |
/** There are plenty of useful methods we could define, but folds provides | |
an alternative to explicit pattern matching and a basis for other | |
useful things. */ | |
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X | |
} | |
/* Implementations are straight forward: */ | |
final case class FirstOf3[A, B, C](a: A) extends OneOf3[A, B, C] { | |
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X = fax(a) | |
} | |
final case class SecondOf3[A, B, C](b: B) extends OneOf3[A, B, C] { | |
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X = fbx(b) | |
} | |
final case class ThirdOf3[A, B, C](c: C) extends OneOf3[A, B, C] { | |
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X = fcx(c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment