Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Created May 3, 2018 09:24
Show Gist options
  • Select an option

  • Save JoolsF/cdd4c3e144331709112b8bd48fe13168 to your computer and use it in GitHub Desktop.

Select an option

Save JoolsF/cdd4c3e144331709112b8bd48fe13168 to your computer and use it in GitHub Desktop.
Lift example 2
def lift3[A, B, C, D](f: Function3[A, B, C, D]): Function3[Option[A], Option[B], Option[C], Option[D]] = {
(oa: Option[A], ob: Option[B], oc: Option[C]) =>
for {
a <- oa
b <- ob
c <- oc
} yield f(a, b, c)
}
case class Foo(a: Int, b: Int, c: Int)
def getFoo(a: Int, b: Int, c: Int): Foo = Foo(a, b, c)
val liftGetFoo: (Option[Int], Option[Int], Option[Int]) => Option[Foo] = lift3(getFoo)
val a = Option(1)
val b = Option(2)
val c = Option(3)
liftGetFoo(a, b, c) // Some(Foo(1,2,3))
liftGetFoo(a, b, None ) // None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment