Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Created November 23, 2018 08:23
Show Gist options
  • Save ScalaWilliam/9fb19ea6153c1e25865e4d91b30614e4 to your computer and use it in GitHub Desktop.
Save ScalaWilliam/9fb19ea6153c1e25865e4d91b30614e4 to your computer and use it in GitHub Desktop.
def of[F[_]](pf: PartialFunction[Request[F], HttpRoutes[F]])(
implicit F: Sync[F]): HttpRoutes[F] = {
Kleisli { req: Request[F] =>
OptionT.fromOption[F](pf.lift(req).map(_.apply(req))).flatMap(identity)
}
}
// but we'd like to do multiple PartialFunctions here actually?
// and a PF is already a Kleisli of Option
def apply[F[_]: Monad, UserId](
f: Kleisli[F, Request[F], Option[HttpRoutes[F]]]): HttpRoutes[F] = {
Kleisli { request: Request[F] =>
OptionT
.liftF(f(request))
.map(_.map(_.apply(request)))
.flatMap(x => OptionT.fromOption[F](x))
.flatMap(identity)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment