-
-
Save tomatophobia/b0c614bd31acedee4175f46cc55d4bb4 to your computer and use it in GitHub Desktop.
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
| case class FutureOption[A](value: Future[Option[A]]) | |
| type HttpRoutes = Http[FutureOption] | |
| object HttpRoutes { | |
| def of(pf: PartialFunction[Request, Future[Response]]): HttpRoutes = | |
| Kleisli(req => FutureOption(pf.lift(req).sequence)) | |
| } | |
| implicit val monadForFutureOption: Monad[FutureOption] = | |
| new Monad[FutureOption] with StackSafeMonad[FutureOption] { | |
| def pure[A](x: A) = FutureOption(Future.successful(Some(x))) | |
| def flatMap[A, B](fa: FutureOption[A])(f: A => FutureOption[B]): FutureOption[B] = { | |
| FutureOption(fa.value.flatMap { | |
| case Some(a) => f(a).value | |
| case None => Future.successful(None) | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment