Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created August 4, 2020 15:26
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 djspiewak/1b92a6e338f4e1537692e748c54b9743 to your computer and use it in GitHub Desktop.
Save djspiewak/1b92a6e338f4e1537692e748c54b9743 to your computer and use it in GitHub Desktop.
def ifElseM[F[_]: FlatMap, A](branches: (F[Boolean], F[A])*, els: F[A]): F[A] = {
def loop(branches: List[(F[Boolean], F[A])]): F[A] =
branches match {
case (cond, conseq) :: tail =>
cond.flatMap(if (_) conseq else loop(tail))
case Nil =>
els
}
loop(branches.toList)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment