Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Monad Transformers down to earth - Scala Days 2...

Monad Transformers down to earth - Scala Days 2017 - Copenhagen

Gabriele Petronella

June 01, 2017
Tweet

More Decks by Gabriele Petronella

Other Decks in Programming

Transcript

  1. THIS TALK: WHAT AND WHY What: The talk I wished

    I attended before banging my head against this @gabro27 Scala Days 2017 - Copenhagen
  2. THIS TALK: WHAT AND WHY What: The talk I wished

    I attended before banging my head against this Why: Because I still remember how it was before knowing it @gabro27 Scala Days 2017 - Copenhagen
  3. THE PROBLEM val x: Future[List[Int]] = ??? futureList.map(list => list.map(f))

    ^ ^ |________________| 2 maps 1 function @gabro27 Scala Days 2017 - Copenhagen
  4. FUNCTOR trait Functor[F[_]] { def map[A, B](fa: F[A])(f: A =>

    B): F[B] } @gabro27 Scala Days 2017 - Copenhagen
  5. FUNCTOR OF FUTURE val futureF = new Functor[Future] { def

    map[A, B](fa: Future[A])(f: A => B): Future[B] = fa.map(f) } @gabro27 Scala Days 2017 - Copenhagen
  6. futureList.map(f) // not really valid scala | // but you

    get the point | Functor[Future[List]].map(futureList)(f) @gabro27 Scala Days 2017 - Copenhagen
  7. IN PRACTICE import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import cats._; import std.future._;

    import std.list._ // create a `Functor[Future[List]]` val futureListF = Functor[Future].compose(Functor[List]) val data: Future[List[Int]] = Future(List(1, 2, 3)) // only one map! futureListF.map(data)(_ + 1) // Future(List(2, 3, 4)) @gabro27 Scala Days 2017 - Copenhagen
  8. IN PRACTICE import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import cats._; import std.future._;

    import std.list._ // create a `Functor[Future[List]]` val futureListF = Functor[Future].compose(Functor[List]) val data: Future[List[Int]] = Future(List(1, 2, 3)) // only one map! futureListF.map(data)(_ + 1) // Future(List(2, 3, 4)) @gabro27 Scala Days 2017 - Copenhagen
  9. ABOUT FLATTENING List(1, 2, 3).map(_ + 1) // List(2, 3,

    4) List(1, 2, 3).map(n => List.fill(n)(n)) // List(List(1), List(2, 2), List(3, 3, 3)) List(1, 2, 3).map(n => List.fill(n)(n)).flatten // List(1, 2, 2, 3, 3, 3) @gabro27 Scala Days 2017 - Copenhagen
  10. FLATMAP flatten ∘ map = flatMap so List(1, 2, 3).map(n

    => List.fill(n)(n)).flatten == List(1, 2, 3).flatMap(n => List.fill(n)(n)) @gabro27 Scala Days 2017 - Copenhagen
  11. IN OTHER WORDS when life gives you F[F[A]] you probably

    wanted flatMap e.g. val f: Future[Future[Int]] = Future(42).map(x => Future(24)) val g: Future[Int] = Future(42).flatMap(x => Future(24)) @gabro27 Scala Days 2017 - Copenhagen
  12. THE 'M' WORD trait Monad[F[_]] { def pure[A](a: A): F[A]

    def map[A, B](fa: F[A])(f: A => B): F[B] def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B] } @gabro27 Scala Days 2017 - Copenhagen
  13. THE 'M' WORD trait Monad[F[_]] { def pure[A](a: A): F[A]

    def map[A, B](fa: F[A])(f: A => B): F[B] def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B] } @gabro27 Scala Days 2017 - Copenhagen
  14. A LESS CONTRIVED EXAMPLE def getUser(name: String): Future[User] def getAddress(user:

    User): Future[Address] val getCity: Future[String] = getUser("Gabriele").flatMap( gab => getAddress(gab).map( address => address.city ) ) @gabro27 Scala Days 2017 - Copenhagen
  15. LET'S COMPREHEND THIS val getCity: Future[String] = for { gab

    <- getUser("Gabriele") address <- getAddress(gab) } yield address.city @gabro27 Scala Days 2017 - Copenhagen
  16. BACK TO THE REAL WORLD def getUser(name: String): Future[User] //

    <- really? def getAddress(user: User): Future[Address] @gabro27 Scala Days 2017 - Copenhagen
  17. BACK TO THE REAL WORLD def getUser(name: String): Future[Option[User]] //

    better def getAddress(user: User): Future[Option[Address]] @gabro27 Scala Days 2017 - Copenhagen
  18. UH, OH... val city: Future[Option[String]] = for { gab <-

    getUser("Gabriele") address <- getAddress(gab) // ! FAIL } yield address.city @gabro27 Scala Days 2017 - Copenhagen
  19. EVENTUALLY val city: Future[Option[String]] = for { gab <- getUser("Gabriele")

    address <- getAddress(gab.get) // ! } yield address.get.city // ! @gabro27 Scala Days 2017 - Copenhagen
  20. OR... val city: Future[Option[String]] = for { maybeUser <- getUser("Gabriele")

    maybeCity <- maybeUser match { case Some(user) => getAddress(user).map(_.map(_.city)) case None => Future.successful(None) } } yield maybeCity @gabro27 Scala Days 2017 - Copenhagen
  21. WHAT WE WOULD REALLY WANT val city: Future[Option[String]] = for

    { gab <- maybeUser <- getUser("Gabriele") address <- maybeAddress <- getAddress(gab) } yield address.city @gabro27 Scala Days 2017 - Copenhagen
  22. WHAT'S THE IMPOSSIBLE PART? // trivial def compose[F[_]: Functor, G[_]:

    Functor]: Functor[F[G[_]]] = ✅ // impossible def compose[M[_]: Monad, N[_]: Monad]: Monad[M[N[_]]] = " // (not valid scala, but you get the idea) @gabro27 Scala Days 2017 - Copenhagen
  23. flatMap FOR Future[Option[A]] val city: Future[Option[String]] = for { maybeUser

    <- getUser("Gabriele") maybeCity <- maybeUser match { case Some(user) => getAddress(user).map(_.map(_.city)) case None => Future.successful(None) } } yield maybeCity @gabro27 Scala Days 2017 - Copenhagen
  24. THE MONAD INTERFACE trait Monad[F[_]] { def pure[A](a: A): F[A]

    def map[A, B](fa: F[A])(f: A => B): F[B] def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B] } @gabro27 Scala Days 2017 - Copenhagen
  25. new Monad[FutOpt] { def pure[A](a: => A): FutOpt[A] = FutOpt(a.pure[Option].pure[Future])

    def map[A, B](fa: FutOpt[A])(f: A => B): FutOpt[B] = FutOpt(fa.value.map(optA => optA.map(f))) def flatMap[A, B](fa: FutOpt[A])(f: A => FutOpt[B]): FutOpt[B] = FutOpt(fa.value.flatMap(opt => opt match { case Some(a) => f(a).value case None => (None: Option[B]).pure[Future] })) } @gabro27 Scala Days 2017 - Copenhagen
  26. AND USE val f: FutOpt[String] = for { gab <-

    FutOpt(getUser("Gabriele")) address <- FutOpt(getAddress(gab)) } yield address.city // ! val city: Future[Option[String]] = f.value @gabro27 Scala Days 2017 - Copenhagen
  27. new Monad[ListOpt] { def pure[A](a: => A): ListOpt[A] = ListOpt(a.pure[Option].pure[List])

    def map[A, B](fa: ListOpt[A])(f: A => B): ListOpt[B] = ListOpt(fa.value.map(optA => optA.map(f))) def flatMap[A, B](fa: ListOpt[A])(f: A => ListOpt[B]): ListOpt[B] = ListOpt(fa.value.flatMap(opt => opt match { case Some(a) => f(a).value case None => (None: Option[B]).pure[List] })) } @gabro27 Scala Days 2017 - Copenhagen
  28. new Monad[FutOpt] { def pure[A](a: => A): FutOpt[A] = FutOpt(a.pure[Option].pure[Future])

    def map[A, B](fa: FutOpt[A])(f: A => B): FutOpt[B] = FutOpt(fa.value.map(optA => optA.map(f))) def flatMap[A, B](fa: FutOpt[A])(f: A => FutOpt[B]): FutOpt[B] = FutOpt(fa.value.flatMap(opt => opt match { case Some(a) => f(a).value case None => (None: Option[B]).pure[Future] })) } @gabro27 Scala Days 2017 - Copenhagen
  29. new Monad[FutOpt] { def pure[A](a: => A): FutOpt[A] = FutOpt(a.pure[Option].pure[Future])

    def map[A, B](fa: FutOpt[A])(f: A => B): FutOpt[B] = FutOpt(fa.value.map(optA => optA.map(f))) def flatMap[A, B](fa: FutOpt[A])(f: A => FutOpt[B]): FutOpt[B] = FutOpt(fa.value.flatMap(opt => opt match { case Some(a) => f(a).value case None => (None: Option[B]).pure[Future] })) } @gabro27 Scala Days 2017 - Copenhagen
  30. MEET OptionT val f: OptionT[Future, String] = for { gab

    <- OptionT(getUser("Gabriele")) address <- OptionT(getAddress(gab)) } yield address.city // ! val city: Future[Option[String]] = f.value @gabro27 Scala Days 2017 - Copenhagen
  31. ANOTHER EXAMPLE def getUser(id: String): Future[Option[User]] = ??? def getAge(user:

    User): Future[Int] = ??? def getNickname(user: User): Option[String] = ??? val lameNickname: Future[Option[String]] = ??? // e.g. Success(Some("gabro27")) @gabro27 Scala Days 2017 - Copenhagen
  32. I KNOW THE TRICK! val lameNickname: OptionT[Future, String]] = for

    { user <- OptionT(getUser("123")) age <- OptionT(getAge(user)) // sorry, nope name <- OptionT(getName(user)) // sorry, neither } yield s"$name$age" @gabro27 Scala Days 2017 - Copenhagen
  33. DO YOU EVEN LIFT, BRO? val lameNickname: OptionT[Future, String]] =

    for { user <- OptionT(getUser("123")) age <- OptionT.liftF(getAge(user)) name <- OptionT.fromOption(getName(user)) } yield s"$name$age" @gabro27 Scala Days 2017 - Copenhagen
  34. EXAMPLE: UPDATING A USER > check user exists > check

    it can be updated > update it @gabro27 Scala Days 2017 - Copenhagen
  35. THE NAIVE WAY def checkUserExists(id: String): Future[Option[User]] def checkCanBeUpdated(u: User):

    Future[Boolean] def updateUserOnDb(u: User): Future[User] @gabro27 Scala Days 2017 - Copenhagen
  36. PROBLEMS def updateUser(u: User): Future[Option[User]] = checkUserExists.flatMap { maybeUser =>

    maybeUser match { case Some(user) => checkCanBeUpdated(user).flatMap { canBeUpdated => if (canBeUpdated) { updateUserOnDb(u) } else { Future(None) } } case None => Future(None) } } @gabro27 Scala Days 2017 - Copenhagen
  37. MORE PROBLEMS (DETAILED ERRORS) case class MyError(msg: String) def updateUser(user:

    User): Future[Either[MyError, User]] = checkUserExists(user.id).flatMap { maybeUser => maybeUser match { case Some(user) => checkCanBeUpdated(user).flatMap { canBeUpdated => if (canBeUpdated) { updateUserOnDb(u).map(_.right) } else { Future(MyError("user cannot be updated").left) } } case None => Future(MyError("user not existing").left) } } } @gabro27 Scala Days 2017 - Copenhagen
  38. HOW ABOUT case class MyError(msg: String) type ResultT[F[_], A] =

    EitherT[F, MyError, A]] type FutureResult[A] = ResultT[Future, A] @gabro27 Scala Days 2017 - Copenhagen
  39. def checkUserExists(id: String): FutureResult[User] = Future { if (id ===

    "123") User("123").right else MyError("sorry, no user").left } def checkCanBeUpdated(u: User): FutureResult[User] = ??? def updateUserOnDb(u: User): FutureResult[User] = ??? @gabro27 Scala Days 2017 - Copenhagen
  40. BETTER? def updateUser(user: User): FutureResult[User] = for { user <-

    checkUserExists(user.id) _ <- checkCanBeUpdated(user) updatedUser <- updateUser(user) } yield updatedUser @gabro27 Scala Days 2017 - Copenhagen
  41. TIP #1 stacking more than two monads gets bad really

    quickly @gabro27 Scala Days 2017 - Copenhagen
  42. EXAMPLE1 val effect: OptionT[EitherT[Task, String, ?], String] = for {

    first <- readName.liftM[EitherT[?[_], String, ?]].liftM[OptionT] last <- readName.liftM[(EitherT[?[_], String, ?]].liftM[OptionT] name <- if ((first.length * last.length) < 20) OptionT.some[EitherT[Task, String, ?], String](s"$first $last") else OptionT.none[EitherT[Task, String, ?], String] _ <- (if (name == "Daniel Spiewak") EitherT.fromDisjunction[Task](\/.left[String, Unit]("your kind isn't welcome here")) else EitherT.fromDisjunction[Task](\/.right[String, Unit](()))).liftM[OptionT] _ <- log(s"successfully read in $name").liftM[EitherT[?[_], String, ?]].liftM[OptionT] } yield name 1 from djspiewak/emm @gabro27 Scala Days 2017 - Copenhagen
  43. TIP #2 keep your transformers for youself def publicApiMethod(x: String):

    OptionT[Future, Int] = ! def publicApiMethod(x: String): Future[Option[Int]] = " by the way val x: OptionT[Future, Int] = OptionT(Future(Option(42))) val y: Future[Option[Int]] = x.value // Future(Option(42)) @gabro27 Scala Days 2017 - Copenhagen
  44. TIP #3 ! Perf! Wrapping/unwrapping isn't cheap, so if you're

    concerned about performance, consider benchmarking your code. @gabro27 Scala Days 2017 - Copenhagen
  45. TIP #4 Use them as a ""local optimization"". In case

    your problem is not "local", consider alternative approaches. @gabro27 Scala Days 2017 - Copenhagen
  46. MONAD TRANSFORMERS: TAKEAWAYS > they end with T > F[G[X]]

    becomes GT[F[_], X] > can be stacked undefinitely, but gets awkward > they are a tool for working with stacked monads @gabro27 Scala Days 2017 - Copenhagen
  47. FREE MONADS > clearly separate structure and interpretation > effects

    are separated from program definition http://typelevel.org/cats/datatypes/freemonad.html @gabro27 Scala Days 2017 - Copenhagen
  48. EFF https://github.com/atnos-org/eff-cats "Extensible effects are an alternative to monad transformers

    for computing with effects in a functional way" based on Freer Monads, More Extensible Effects by Oleg Kiselyov @gabro27 Scala Days 2017 - Copenhagen