Skip to content

Instantly share code, notes, and snippets.

@n4to4
Created June 26, 2017 03:16
Show Gist options
  • Save n4to4/e4b6b445971b83e9c71af4900dfd1c25 to your computer and use it in GitHub Desktop.
Save n4to4/e4b6b445971b83e9c71af4900dfd1c25 to your computer and use it in GitHub Desktop.
lift
import cats._, cats.data._, cats.implicits._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Await, duration}
object Main extends App {
implicit class Ops[In](in: In) {
def |>[Out](f: In => Out): Out = f(in)
}
def liftFutureOption[A](f: Future[Option[A]]) = OptionT(f)
def liftFuture[A](f: Future[A]) = OptionT.liftF(f)
def liftOption[A](o: Option[A]) = OptionT(o.pure[Future])
def lift[A](a: A) = liftOption(Some(a))
def getA = Future.successful(Some(1))
def getB = Future.successful(Some(2))
def getC = Future.successful(3)
def getD = Some(4)
val result = for {
a <- getA |> liftFutureOption
b <- getB |> liftFutureOption
c <- getC |> liftFuture
d <- getD |> liftOption
e <- 10 |> lift
} yield e * (a * b) / (c * d)
println(Await.result(result.value, duration.Duration.Inf))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment