Skip to content

Instantly share code, notes, and snippets.

@johnynek
Last active December 10, 2015 18:08
Show Gist options
  • Save johnynek/4472451 to your computer and use it in GitHub Desktop.
Save johnynek/4472451 to your computer and use it in GitHub Desktop.
ExceptionalBijection
def orElse[A,B](fna: A => Option[B])(fni: B => Option[A])(implicit bi: Bijection[A,B]): Bijection[A,B] =
new Bijection[A,B] {
def apply(a: A) = fna(a).getOrElse(bij(a))
override invert(b: B) = fni(b).getOrElse(bij.invert(b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment