2. ...is a control mechanism for sequencing computations.
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
3. ...is a control mechanism for sequencing computations.
ExecutorService es =
Executors.newSingleThreadExecutor();
es.submit(() -> System.out.println("Message 1"));
es.submit(() -> System.out.println("Message 2"));
es.submit(() -> System.out.println("Message 3"));
es.shutdown();
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
because we can
We changing your state
Imperative
4. ...is a control mechanism for sequencing computations.
Future(println(“Message 1”))
.map(_ => println(“Message 2”))
.map(_ => println(“Message 3”))
ExecutorService es =
Executors.newSingleThreadExecutor();
es.submit(() -> System.out.println("Message 1"));
es.submit(() -> System.out.println("Message 2"));
es.submit(() -> System.out.println("Message 3"));
es.shutdown();
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
because we can
We changing your state
Imperative
Functional
approach
WELCOME TO
5. ...is a control mechanism for sequencing computations.
trait MyLittleClassyMonad { // lets call it just F[_]
}
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
6. ...is a control mechanism for sequencing computations.
Value pure
trait MyLittleClassyMonad { // lets call it just F[_]
def pure[A](value: A): F[A]
}
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
Value
7. ...is a control mechanism for sequencing computations.
trait MyLittleClassyMonad { // lets call it just F[_]
def pure[A](value: A): F[A]
def flatMap[A, B](value: F[A])(f: A => F[B]): F[B]
}
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
flatMapValue A Value B
8. ...have some rules!
LEFT IDENTITY:
a.pure.flatMap(f) == f(a)
RIGHT IDENTITY:
m.flatMap(pure) == m
ASSOCIATIVITY:
m.flatMap(f).flatMap(g) == m.flatMap(x => f(x).flatMap(g))
MONAD
8MINUTESOFFUNCTIONALPRIMITIVES
9. ...every monad is a functor
trait FancyLittleFunctor {
def map[A, B](value: F[A])(f: A => B): F[B]
}
FUNCTOR
8MINUTESOFFUNCTIONALPRIMITIVES
10. ...every monad is a functor
trait FancyLittleFunctor {
def map[A, B](value: F[A])(f: A => B): F[B]
}
trait FancyLittleFunctor extends MyLittleClassyMonad {
def map[A, B](value: F[A])(f: A => B): F[B] =
flatMap(value)(v => pure(f(v)))
}
FUNCTOR
8MINUTESOFFUNCTIONALPRIMITIVES
11. ...when fail fast is not your case.
trait GorgeousCartesian {
def product[A, B](fa: F[A], fb: F[B]): F[(A, B)]
}
CARTESIANS
8MINUTESOFFUNCTIONALPRIMITIVES
Value A Value B
Values
A and B
product