Skip to content

Latest commit

 

History

History
51 lines (28 loc) · 1.06 KB

Monad.md

File metadata and controls

51 lines (28 loc) · 1.06 KB
<R> Monad<R> map(Transformer<? super T, ? extends R> f) {
  return flatMap(x -> Monad.of(f.transform(x)));
}

> Composition Law of Functor
m.map(x -> f(x)).map(x -> g(x))
m.flatMap(x -> Monad.of(f(x))).flatMap(x -> Monad.of(g(x))) by the implementation
m.flatMap(x -> Monad.of(f(x)).flatMap(x -> Monad.of(g(x)))) by Monad's Associative Law
m.flatMap(x -> Monad.of(g(f(x)))) by Monad's Left Identity Law
m.map(x -> g(f(x))) by the implementation

ASSOCIATIVITY

BiFunction<Integer, Integer, Integer> accumulator = (a, x) -> { return 2*a + x; }

BinaryOperator combiner = (a1, a2) -> { return a1 + a2; }

Stream.of(1, 2, 3, 4) .reduce(0, (a,x) -> 2*a + x, (a1, a2) -> a1 + a2 );

Check associativity

Accumulator is not associative: 2*((21)+2) + 3 vs (21) + ((2*2) + 3)

NOT this as it assumes commutativity: 2*((21)+2)+3 = 11 vs (23)+((2*1)+2)=10 Combiner is associative: (1+2)+3 = 1+(2+3)

Check compatibility

c(u, a(0,t)) is NOT not a(0, u) + a(0, t) c(u, a(0,t)) = u + a(0, t) = 2 + ((20)+1) = 3 a(u, t) = 2u + t = 22 + 1 = 5