with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the...
Transcript of with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the...
![Page 1: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/1.jpg)
Unlocking magic of Monads with Java 8
Oleg Šelajev @shelajev
ZeroTurnaround
![Page 2: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/2.jpg)
Who am I
@shelajev
![Page 3: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/3.jpg)
What do we want to achieve?> have fun while learning stuff
> understand the concept of Monad
> get generic constructs for Monads
> solve a real-world problem
ignore lack of “ad hoc” polymorphism and other slightly relevant facts
![Page 4: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/4.jpg)
What do we want to achieve?> have fun while learning stuff
> understand the concept of Monad
> get generic constructs for Monads
> solve a real-world problem
> ignore lack of “ad hoc” polymorphism and other slightly relevant facts
![Page 5: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/5.jpg)
![Page 6: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/6.jpg)
Java 8: lambda recap
@FunctionalInterfacepublic interface Function<T, R> { R apply(T t); }
![Page 7: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/7.jpg)
Java 8: lambda recap
Function<String, Integer> f = Integer::valueOf;
![Page 8: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/8.jpg)
Java 8: lambda recap
String prefix = “JFokus: "; Function<String, Integer> f = (str) -> { System.out.println(prefix + str); return str.hashCode(); };
![Page 9: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/9.jpg)
Death by 1000 tutorials
![Page 10: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/10.jpg)
Problem driven education
![Page 11: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/11.jpg)
Problem statement
$("#button").fadeIn("slow", function() { console.log("hello world"); } );
![Page 12: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/12.jpg)
Problem statementobject.interact("option1", () -> { object.doThing("fast", () -> { if(wasSuccessful()) { object.celebrate(100, TimeUnit.SECONDS, () -> { System.out.println("Back to work"); }); } else { object.beSad(":(", () -> { System.out.println("Still back to work"); }); }); });
![Page 13: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/13.jpg)
Problem statement
![Page 14: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/14.jpg)
Kinda solution
object.interact("option1") .then((o) -> o.doThing("fast")) .then((o) -> o.celebrate(100, SECONDS, () -> { System.out.println("Back to work”); })) .or((o) -> o.beSad(":("));
![Page 15: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/15.jpg)
Type: async result
> java.util.concurrent.Future<V>
> boolean isDone(); > V get() … > V get(long timeout, TimeUnit unit)
![Page 16: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/16.jpg)
Type: async result
> java.util.concurrent.Future<V>
> boolean isDone(); > V get() … > V get(long timeout, TimeUnit unit)
Can we do
better?
![Page 17: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/17.jpg)
Monads to the rescue
![Page 18: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/18.jpg)
Monads to the rescue
![Page 19: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/19.jpg)
Oh my…> a monad in X is just a
monoid in the category of endofunctors of X, with product × replaced by composition of endofunctors and unit set by the identity endofunctor.
![Page 20: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/20.jpg)
It is known
Every cook can understand, compile and use monads…
V. Lenin (1923)
![Page 21: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/21.jpg)
![Page 22: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/22.jpg)
Monads: intuition
> wrapping things
> chaining functions on those things
> monad is a type
![Page 23: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/23.jpg)
![Page 24: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/24.jpg)
Wrapping: return / pure
> Take instance of “a”, return: “m a”
> Constructor / Factory method
![Page 25: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/25.jpg)
Pure in Java
public interface Monad<V> { Monad<V> pure(V value); }
![Page 26: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/26.jpg)
Chaining: bind / (>>=)
> take:
> monad: “m a”
> function: “a => m b”
> return: monad “m b”
![Page 27: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/27.jpg)
Bind in Java
public interface Monad<V> { Monad<V> pure(V v); <R> Monad<R> bind(Function<V, Monad<R>> f); }
![Page 28: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/28.jpg)
Hacking time
> Promise<V> - result of async computation
> Kinda like Future<V>
> supports chaining functions: bind
![Page 29: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/29.jpg)
Imagined Promise<V>
> Future operations > p.invokeWithException(Throwable t); > p.invoke(V v); > p.onRedeem(Action<Promise<V>> callback);
![Page 30: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/30.jpg)
Promise<V>: pure
public static <V> Promise<V> pure (final V v) { Promise<V> p = new Promise<>(); p.invoke(v); return p; }
![Page 31: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/31.jpg)
Promise<V>: bindpublic <R> Promise<R> bind(final Function<V, Promise<R>> function) { Promise<R> result = new Promise<>(); this.onRedeem(callback -> { V v = callback.get(); Promise<R> applicationResult = function.apply(v); applicationResult.onRedeem(c -> { R r = c.get(); result.invoke(r); }); return result; }
![Page 32: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/32.jpg)
Promise<V>: get
public V get() throws InterruptedException, ExecutionException { taskLock.await(); if (exception != null) { throw new ExecutionException(exception); } return result; }
![Page 33: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/33.jpg)
ExamplePromise<String> p = Async.submit(() -> { return "hello world"; }); Promise<Integer> result = p.bind(string -> Promise.pure(Integer.valueOf(string.hashCode()))); System.out.println("HashCode = " + result.get());
![Page 34: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/34.jpg)
Checkpoint
> Promise - represents async computation
> Values and exceptions handling
> Chaining of functions
![Page 35: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/35.jpg)
Wait, is that it?
> Monad vs. Instance of monad
![Page 36: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/36.jpg)
Typeclass? Higher functions?
> Common interface
> Generic functions over all monads
![Page 37: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/37.jpg)
Greatness
> Common operations for Monads
> sequence, zip
> Limited under parametrised polymorphism in Java
![Page 38: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/38.jpg)
Sequence
Monad<List<V>> sequence(Monad<V>... monads);
![Page 39: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/39.jpg)
Some languages have it easier than others
> m >> n = m >>= \_ -> n
> f x a = … a
> Implicit get()
![Page 40: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/40.jpg)
Monad in Java
public interface Monad<V> { Monad<V> pure(V v); <R> Monad<R> bind(Function<V, Monad<R> f); V get(); }
![Page 41: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/41.jpg)
> One does not simply call itself a monad!
![Page 42: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/42.jpg)
![Page 43: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/43.jpg)
Laws (don’t be scared)
> return a >>= f ≡ f a
> m >>= return ≡ m
> (m >>= f) >>= g ≡ m >>= (\x -> f x >>= g)
![Page 44: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/44.jpg)
Left identity
> pure(v).bind(f) ≡ f.apply(v)
![Page 45: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/45.jpg)
Right identity
> m.bind(m::pure) ≡ m
![Page 46: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/46.jpg)
Associativity
> m.bind(f).bind(g) ≡ m.bind(
(v) -> f.apply(v).bind(g))
![Page 47: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/47.jpg)
Some have it easy
> Referential transparency
> Partial application
> ≡ is easy
![Page 48: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/48.jpg)
Mortal platforms
> No referential transparency
> f.apply(v) != f.apply(v)
> equals() + hashcode()
![Page 49: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/49.jpg)
Defining ≡ for Java
> Side effects are similar
> m.get() observes the same values
> values or exceptions
![Page 50: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/50.jpg)
Promise: left identityFunction<Integer, Promise<Boolean>> f = (x) -> { return submit(() -> x % 2 == 0); }; Integer val = new Integer(100); assertEquals(Promise.pure(val).bind(f).get(), f.apply(val).get());
![Page 51: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/51.jpg)
Promise: right identity
Integer val = new Integer(100000); Promise<Integer> p = Promise.pure(val).bind(Promise::pure); assertEquals(val, p.get()); assertEquals(identityHashCode(val), identityHashCode(p.get()));
![Page 52: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/52.jpg)
Quality software
> java.util.concurrent.CompletableFuture
> thenApply(Function / Consumer / etc)
> thenApplyAsync(Function / etc)
> Async => FJP.common()
![Page 53: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/53.jpg)
![Page 54: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/54.jpg)
Optional purestatic <T> Optional<T> of(T value) { return new Optional<>(value); }
static <T> Optional<T> ofNullable(T value) { return value == null ? empty() : of(value); }
![Page 55: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/55.jpg)
Optional bindpublic<U> Optional<U> flatMap(Function<T, Optional<U>> mapper) { Objects.requireNonNull(mapper); if (!isPresent()) return empty(); else { return Objects.requireNonNull(mapper.apply(value)); } }
![Page 56: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/56.jpg)
Optional bindpublic<U> Optional<U> map(Function<T, U> mapper) { Objects.requireNonNull(mapper); if (!isPresent()) return empty(); else { return Optional.ofNullable(mapper.apply(value)); } }
![Page 57: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/57.jpg)
![Page 58: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/58.jpg)
![Page 59: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/59.jpg)
Free STUFF
BOOTH #8
![Page 60: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/60.jpg)
Free STUFF
http://bit.ly/tshirt-oleg
![Page 62: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/62.jpg)
Minority report
> Alternative definition of Monads:
> fmap :: (a -> b) -> f a -> f b
> join :: m (m a) -> m a
![Page 63: with Java 8 › jfokus15 › preso › Monads.pdf · > a monad in X is just a monoid in the category of ... endofunctors and unit set by the identity endofunctor. It is known Every](https://reader036.fdocuments.in/reader036/viewer/2022081522/5f2746aebcc2ef3942491700/html5/thumbnails/63.jpg)
Exercises
> Implement (>>=) in terms of fmap and join.
> Now implement join and fmap in terms of (>>=) and return.