Skip to content

Instantly share code, notes, and snippets.

@gabro
Created February 25, 2017 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabro/792ed58a35bb01da4ccea07f64ff69d6 to your computer and use it in GitHub Desktop.
Save gabro/792ed58a35bb01da4ccea07f64ff69d6 to your computer and use it in GitHub Desktop.
Monad Transformers for the working programmer
import cats.data.OptionT, cats.std.future._
def findAddressByUserId(id: Long): Future[Option[Address]] =
(for {
user <- OptionT(findUserById(id))
address <- OptionT(findAddressByUser(user))
} yield address).value
def findUserById(id: Long): OptionT[Future, User] =
OptionT { ??? }
def findAddressByUser(user: User): OptionT[Future, Address] =
OptionT { ??? }
def findAddressByUserId(id: Long): OptionT[Future, Address]] =
for {
user <- findUserById(id))
address <- findAddressByUser(user))
} yield address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment