Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Monad Transformers down to earth - Scala Swarm ...
Search
Gabriele Petronella
June 23, 2017
Programming
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Monad Transformers down to earth - Scala Swarm 2017 - Porto
Gabriele Petronella
June 23, 2017
More Decks by Gabriele Petronella
See All by Gabriele Petronella
Design System Adventures in React - ReactJS Day 2024
gabro
0
150
Design System Adventures in React
gabro
1
140
Casting Metals
gabro
0
390
Functional Programming in front-end applications
gabro
1
250
Functional Programming in Front-end Applications
gabro
3
220
How to get away with Functional Programming in front-end applications
gabro
3
1.6k
Bridging the tooling gap with Scala.js
gabro
0
310
Monad Transformers Down to Earth
gabro
2
2.8k
Move fast and fix things
gabro
0
380
Other Decks in Programming
See All in Programming
RTSPクライアントを自作してみた話
simotin13
0
630
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
230
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
180
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.4k
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
120
A2UI という光を覗いてみる
satohjohn
1
150
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
7k
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
360
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.5k
AI時代のUIはどこへ行く?その2!
yusukebe
22
7.5k
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
300
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
2
740
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
The Cult of Friendly URLs
andyhume
79
6.9k
Facilitating Awesome Meetings
lara
57
7k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
66
55k
Building AI with AI
inesmontani
PRO
1
1.1k
Making the Leap to Tech Lead
cromwellryan
135
9.9k
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
580
Rails Girls Zürich Keynote
gr2m
96
14k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
950
Transcript
GABRIELE PETRONELLA MONAD TRANSFORMERS DOWN TO EARTH
ME, HI! @gabro27 Scala Swarm 2017
STUFF I DO @gabro27 Scala Swarm 2017
THIS TALK: WHAT AND WHY @gabro27 Scala Swarm 2017
THIS TALK: WHAT AND WHY What: The talk I wished
I attended before banging my head against this @gabro27 Scala Swarm 2017
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 Swarm 2017
A QUESTION @gabro27 Scala Swarm 2017
@gabro27 Scala Swarm 2017
THE PROBLEM val x: Future[List[Int]] = ??? futureList.map(list => list.map(f))
^ ^ |________________| 2 maps 1 function @gabro27 Scala Swarm 2017
CAN WE DO BETTER? @gabro27 Scala Swarm 2017
INDENT! futureList.map { list => list.map(f) } @gabro27 Scala Swarm
2017
None
FUNCTOR trait Functor[F[_]] { def map[A, B](fa: F[A])(f: A =>
B): F[B] } @gabro27 Scala Swarm 2017
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 Swarm 2017
future.map(f) list.map(f) | | | | Functor[Future].map(future)(f) Functor[List].map(list)(f) @gabro27 Scala
Swarm 2017
futureList.map(f) // not really valid scala | // but you
get the point | Functor[Future[List]].map(futureList)(f) @gabro27 Scala Swarm 2017
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 Swarm 2017
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 Swarm 2017
CATS https://github.com/typelevel/cats @gabro27 Scala Swarm 2017
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 Swarm 2017
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 Swarm 2017
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 Swarm 2017
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 Swarm 2017
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 Swarm 2017
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 Swarm 2017
LET'S COMPREHEND THIS val getCity: Future[String] = for { gab
<- getUser("Gabriele") address <- getAddress(gab) } yield address.city @gabro27 Scala Swarm 2017
LESSONS monads allow sequential execution monads can squash F[F[A]] into
F[A] @gabro27 Scala Swarm 2017
QUESTIONS? @gabro27 Scala Swarm 2017
WAIT A SECOND... @gabro27 Scala Swarm 2017
WHAT ABOUT F[G[X]] @gabro27 Scala Swarm 2017
BACK TO THE REAL WORLD def getUser(name: String): Future[User] //
<- really? def getAddress(user: User): Future[Address] @gabro27 Scala Swarm 2017
BACK TO THE REAL WORLD def getUser(name: String): Future[Option[User]] //
better def getAddress(user: User): Future[Option[Address]] @gabro27 Scala Swarm 2017
UH, OH... val city: Future[Option[String]] = for { gab <-
getUser("Gabriele") address <- getAddress(gab) // ! FAIL } yield address.city @gabro27 Scala Swarm 2017
EVENTUALLY val city: Future[Option[String]] = for { gab <- getUser("Gabriele")
address <- getAddress(gab.get) // ! } yield address.get.city // ! @gabro27 Scala Swarm 2017
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 Swarm 2017
WHAT WE WOULD REALLY WANT val city: Future[Option[String]] = for
{ gab <- maybeUser <- getUser("Gabriele") address <- maybeAddress <- getAddress(gab) } yield address.city @gabro27 Scala Swarm 2017
futureUser.flatMap(f) maybeUser.flatMap(f) | | | | Monad[Future].flatMap(futureUser)(f) Monad[Option].flatMap(maybeUser)f) @gabro27 Scala
Swarm 2017
futureMaybeUser.flatMap(f) | | Monad[Future[Option]].flatMap(f) @gabro27 Scala Swarm 2017
futureMaybeUser.flatMap(f) | | Monad[Future[Option]].flatMap(f) @gabro27 Scala Swarm 2017
MONADS DO NOT COMPOSE HTTP://BLOG.TMORRIS.NET/POSTS/ MONADS-DO-NOT-COMPOSE/ @gabro27 Scala Swarm 2017
None
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 Swarm 2017
MONADS DO NOT COMPOSE GENERICALLY @gabro27 Scala Swarm 2017
BUT YOU CAN COMPOSE THEM SPECIFICALLY @gabro27 Scala Swarm 2017
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 Swarm 2017
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 Swarm 2017
case class FutOpt[A](value: Future[Option[A]) @gabro27 Scala Swarm 2017
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 Swarm 2017
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 Swarm 2017
WHAT IF def getUsers(query: String): List[Option[User]] @gabro27 Scala Swarm 2017
case class ListOpt[A](value: List[Option[A]) @gabro27 Scala Swarm 2017
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 Swarm 2017
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 Swarm 2017
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 Swarm 2017
A MORE GENERIC APPROACH case class WhateverOpt[A, W[_]](value: W[Option[A]]) @gabro27
Scala Swarm 2017
MEET OptionT OptionT[F[_], A] ^ |___ any monad @gabro27 Scala
Swarm 2017
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 Swarm 2017
IN GENERAL Foo[Bar[X]] becomes BarT[Foo, X] @gabro27 Scala Swarm 2017
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 Swarm 2017
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 Swarm 2017
None
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 Swarm 2017
EXAMPLE: UPDATING A USER > check user exists > check
it can be updated > update it @gabro27 Scala Swarm 2017
THE NAIVE WAY def checkUserExists(id: String): Future[Option[User]] def checkCanBeUpdated(u: User):
Future[Boolean] def updateUserOnDb(u: User): Future[User] @gabro27 Scala Swarm 2017
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 Swarm 2017
DETAILED ERRORS from Option[User] to Either[MyError, User] @gabro27 Scala Swarm
2017
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 Swarm 2017
! F[Either[A, B]] @gabro27 Scala Swarm 2017
! EitherT[F[_], A, B] @gabro27 Scala Swarm 2017
HOW ABOUT case class MyError(msg: String) type ResultT[F[_], A] =
EitherT[F, MyError, A] type FutureResult[A] = ResultT[Future, A] @gabro27 Scala Swarm 2017
SOME HELPERS object FutureResult { def apply[A](a: A): FutureResult[A] =
apply(Future.successful(a)) def apply[A](fa: Future[A]): FutureResult[A] = EitherT.liftT(fa) def apply[A](e: Either[MyError, A]): FutureResult[A] = EitherT.fromEither(e) } @gabro27 Scala Swarm 2017
def checkUserExists(id: String): FutureResult[User] = FutureResult { if (id ===
"123") User("123").asRight else MyError("sorry, no user").asLeft } def checkCanBeUpdated(u: User): FutureResult[Unit] = def updateUserOnDb(u: User): FutureResult[User] = ??? @gabro27 Scala Swarm 2017
BETTER? def updateUser(user: User): FutureResult[User] = for { user <-
checkUserExists(user.id) _ <- checkCanBeUpdated(user) updatedUser <- updateUser(user) } yield updatedUser @gabro27 Scala Swarm 2017
PERSONAL TIPS @gabro27 Scala Swarm 2017
TIP #1 stacking more than two monads gets bad really
quickly @gabro27 Scala Swarm 2017
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 Swarm 2017
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 Swarm 2017
TIP #3 ! Perf! Wrapping/unwrapping isn't cheap, so if you're
concerned about performance, consider benchmarking your code. @gabro27 Scala Swarm 2017
TIP #4 Use them as a ""local optimization"". In case
your problem is not "local", consider alternative approaches. @gabro27 Scala Swarm 2017
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 Swarm 2017
WHAT ELSE? @gabro27 Scala Swarm 2017
FREE MONADS > clearly separate structure and interpretation > effects
are separated from program definition http://typelevel.org/cats/datatypes/freemonad.html @gabro27 Scala Swarm 2017
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 Swarm 2017
None
questionsT @gabro27 @buildoHQ @scalaitaly @gabro27 Scala Swarm 2017