Created
June 26, 2017 03:16
-
-
Save n4to4/e4b6b445971b83e9c71af4900dfd1c25 to your computer and use it in GitHub Desktop.
lift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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