Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Last active January 11, 2017 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/9e8b2e1e99dc6ca5d80c81a679e84e43 to your computer and use it in GitHub Desktop.
Save chrislewis/9e8b2e1e99dc6ca5d80c81a679e84e43 to your computer and use it in GitHub Desktop.
/** 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