SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
MONAD FACT #4
@philip_schwarzslides by
https://www.slideshare.net/pjschwarz
a monad is an implementation of one of the minimal sets of monadic combinators,
satisfying the laws of associativity and identity
see how compositional responsibilities are distributed in each combinator set
We’ve seen three minimal sets of primitive Monad combinators, and instances of Monad will
have to provide implementations of one of these sets:
• unit and flatMap
• unit and compose
• unit, map, and join
And we know that there are two monad laws to be satisfied, associativity and identity, that can be
formulated in various ways. So we can state plainly what a monad is:
A monad is an implementation of one of the minimal sets of monadic combinators,
satisfying the laws of associativity and identity.
That’s a perfectly respectable, precise, and terse definition. And if we’re being precise, this is the
only correct definition. A monad is precisely defined by its operations and laws; no more, no less.
Runar Bjarnason
@runarorama
Paul Chiusano
@pchiusano
Functional Programming in Scala
by Paul Chiusano and Runar Bjarnason
One of the minimal sets of primitive Monad combinators seen on the previous slide consists of
a unit function and a compose function.
The compose function in question is Kleisli composition.
If you need an introduction to Kleisli composition then see MONAD FACT #2.
If you need an introduction to the unit function then see MONAD FACT #1.
Another set of combinators includes the join function. In Scala this function is known as flatten.
@philip_schwarz
Let’s take the simplest monad, i.e. the identity monad, which does nothing, and let’s define it in terms of Kleisli composition and unit.
The Id monad wraps a value of some type A
Id also acts as the unit function. i.e. to lift the value 3 into the Id monad we use Id(3).
case class Id[A](value: A)
Now we have to come up with a body for the Kleisli composition function (shown below as the infix fish operator >=>):
The body must be a function of type A => Id[C]
a => ???
The only way we can get an Id[C] is by calling g, which takes a B as a parameter. But all we have to work with are the a parameter,
which is of type A, and function f. But that is fine because if we call f with a we get an Id[B] and if we then ask the latter for the B
value that it wraps, we have the B that we need to invoke g.
a => g(f(a).value)
And here is a simple test for the functionSo here is how we define Kleisli composition
implicit class IdFunctionOps[A,B](f: A => Id[B]) {
def >=>[C](g: B => Id[C]): A => Id[C] = ???
}
implicit class IdFunctionOps[A,B](f: A => Id[B]) {
def >=>[C](g: B => Id[C]): A => Id[C] =
a => g(f(a).value)
}
val double: Int => Id[Int] = n => Id(n * 2)
val square: Int => Id[Int] = n => Id(n * n)
assert( (double >=> square)(3) == Id(36))
Here is a simple test for join
Here is a test for our map function
So yes, we have defined the identity monad in terms of Kleisli
composition and unit.
But we want to be able to use the monad in a for comprehension, so
we now have to define a flatMap function and a map function. The
flatMap function can be defined in terms of Kleisli composition:
and map can then be defined in terms of flatMap:
case class Id[A](value: A)
object Id {
implicit class IdFunctionOps[A,B](f: A => Id[B]) {
def >=>[C](g: B => Id[C]): A => Id[C] =
a => g(f(a).value)
}
}
case class Id[A](value: A) {
def flatMap[B](f: A => Id[B]): Id[B] =
(((_:Unit) => this) >=> f)(())
}
case class Id[A](value: A) {
def flatMap[B](f: A => Id[B]): Id[B] =
(((_:Unit) => this) >=> f)(())
def map[B](f: A => B): Id[B] =
this flatMap { a => Id(f(a)) }
}
val increment: Int => Int = n => n + 1
assert( (Id(3) map increment) == Id(4) )
And here is a test for both our map and
flatMap functions
val result =
for {
six <- double(3)
thirtySix <- square(six)
} yield six + thirtySix
assert(result == Id(42))
We can also define join (aka flatten) in terms of
the flatMap function
def join[A](mma: Id[Id[A]]): Id[A] =
mma flatMap identity
assert( join(Id(Id(3))) == Id(3) )
Here is the whole code for the identity monad
In this slide deck we are going to compare the identity monad with the Option monad and the List monad.
How do the functions of the above identity monad, which is defined in terms of Kleisli composition, relate
to the equivalent Option monad functions?
See the next slide for the differences.
case class Id[A](value: A) {
def flatMap[B](f: A => Id[B]): Id[B] =
(((_:Unit) => this) >=> f)(())
def map[B](f: A => B): Id[B] =
this flatMap { a => Id(f(a)) }
}
object Id {
implicit class IdFunctionOps[A,B](f: A => Id[B]) {
def >=>[C](g: B => Id[C]): A => Id[C] =
a => g(f(a).value)
}
def join[A](mma: Id[Id[A]]): Id[A] =
mma flatMap identity
}
val double: Int => Id[Int] = n => Id(n * 2)
val square: Int => Id[Int] = n => Id(n * n)
assert( (double >=> square)(3) == Id(36))
val increment: Int => Int = n => n + 1
assert( (Id(3) map increment) == Id(4) )
assert( join(Id(Id(3))) == Id(3) )
val result =
for {
six <- double(3)
thirtySix <- square(six)
} yield six + thirtySix
assert(result == Id(42))
First of all we see that apart from the obvious swapping of Id for Option in their signatures, the flatMap and join functions of
the two monads are identical.
The only difference between the map functions of the two monads are the unit functions that they use: one uses Id and the other
uses Some.
So the only real difference between the two monads is the logic in the fish operator. That makes sense, since the monads are
defined in terms of unit and Kleisli composition, and since unit is a very simple function.
@philip_schwarz
The same is true of the differences between the functions of the Id monad and those of the List monad: the differences are in
the fish operator;
The apparent additional difference between the map functions is only due to the fact that we are using Cons(x,Nil) as a unit
function rather List(x), i.e. some singleton list constructor that we could define.
Let’s now turn to the function that differentiates the monads, i.e. Kleisli composition (the fish operator)
The composite function that it returns (the composition of f and g) has the following responsibilities (let’s call them
compositional responsibilities):
1) use f to compute a first value wrapped in a functional effect
2) dig underneath the wrapper to access the first value, discarding the wrapper
3) Use g to compute, using the first value, a second value also wrapped in a functional effect
4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two
functional effects
As a slight variation on that, we can replace ‘wrapped in’ with ‘in the context of’
1) use f to compute a first value in the context of a functional effect
2) dig inside the context to access the first value, discarding the context
3) Use g to compute, using the first value, a second value also in the context of a functional effect
4) return a third value in the context of a functional effect that represents the composition (combination) of the first two
functional effects
implicit class IdFunctionOps[A,B](f: A => Id[B]) {
def >=>[C](g: B => Id[C]): A => Id[C] =
a => ???
}
Here are the Kleisli composition functions of the three monads (their fish operators).
Notice how different they are. The one in the identity monad seems to do almost nothing, the one in the Option monad seems to do a bit more
work, and the one in the List monad does quite a bit more.
See the next slide for some test code for the Option monad and the List monad.
object Option {
implicit class OptionFunctionOps[A, B](f: A => Option[B]) {
def >=>[C](g: B => Option[C]): A => Option[C] =
a => f(a) match {
case Some(b) => g(b)
case None => None
}
}
}
object Id {
implicit class IdFunctionOps[A,B](f: A => Id[B]) {
def >=>[C](g: B => Id[C]): A => Id[C] =
a => g(f(a).value)
}
}
sealed trait List[+A] {
def foldRight[B](b: B, f: (A,B) => B): B =
this match {
case Nil => b
case Cons(a, tail) =>
f(a, tail.foldRight(zero, f))
}
}
object List {
implicit class ListFunctionOps[A,B](f: A => List[B]) {
def >=>[C](g: B => List[C]): A => List[C] =
a => f(a).foldRight(Nil,
(b:B, cs:List[C]) => concatenate(g(b), cs))
}
def concatenate[A](left: List[A], right: List[A]): List[A] =
left match {
case Nil => right
case Cons(head, tail) => Cons(head, concatenate(tail, right))
}
}
// Tests for Option monad
assert( join(Some(Some(3))) == Some(3) )
val increment: Int => Int = n => n + 1
assert( (Some(3) map increment) == Some(4) )
val double: Int => Option[Int] =
n => if (n % 2 == 1) Some(n * 2) else None
val square: Int => Option[Int] =
n => if (n < 100) Some(n * n) else None
assert( (double >=> square)(3) == Some(36))
val result =
for {
six <- double(3)
thirtySix <- square(six)
} yield six + thirtySix
assert(result == Some(42))
// Tests for List monad
assert( join(Cons(
Cons(1, Cons(2, Nil)),
Cons(
Cons(3, Cons(4, Nil)),
Nil))
) == Cons(1, Cons(2, Cons(3, Cons(4, Nil))) ) )
val increment: Int => Int = n => n + 1
assert( (Cons(1, Cons(2, Cons(3, Cons(4, Nil))) ) map increment)
== Cons(2, Cons(3, Cons(4, Cons(5, Nil))) ) )
val double: Int => List[Int] = n => Cons(n, Cons(n * 2, Nil))
val square: Int => List[Int] = n => Cons(n, Cons(n * n, Nil))
assert( (double >=> square)(3) == Cons(3,Cons(9,Cons(6,Cons(36, Nil)))))
val result =
for {
x <- double(3)
y <- square(x)
} yield Cons(x, Cons(y, Nil))
assert(result == Cons(
Cons(3,Cons(3,Nil)),
Cons(
Cons(3,Cons(9,Nil)),
Cons(
Cons(6,Cons(6,Nil)),
Cons(
Cons(6,Cons(36,Nil)),
Nil)))))
Next, we are going to look at the Kleisli composition functions of Id, Option
and List to see how each of them discharges its compositional responsibilities.
@philip_schwarz
In the special case of the Identity monad, which does nothing, the compositional responsibilities are discharged in a degenerate and
curious way:
1) use f to compute a first value wrapped in a functional effect
just call f
f(a)
2) dig underneath the wrapper to access the first value, discarding the wrapper
digging under the wrapper simply amounts to asking the resulting Id[B] for the B that it is wrapping
f(a).value
3) use g to compute, using the first value, a second value also wrapped in a functional effect
just call g with the first value
g(f(a).value)
4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects
because the effect of the Id monad is nonexistent, there simply is nothing to combine, so just return the second value
g(f(a).value)
implicit class IdFunctionOps[A,B](f: A => Id[B]) {
def >=>[C](g: B => Id[C]): A => Id[C] =
a => g(f(a).value)
}
Next, let’s look at how the compositional responsibilities are discharged in the Option monad:
1) use f to compute a first value wrapped in a functional effect
just call f
f(a)
2) dig underneath the wrapper to access the first value, discarding the wrapper
digging under the wrapper and discarding it is done by pattern matching, destructuring Option[B] to get the wrapped B value
Some(b)
3) use g to compute, using the first value, a second value also wrapped in a functional effect
just call g with the first value
g(b)
4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects
If the 1st effect is that a value is defined then the 3rd value is just the 2nd value and composition of the 1st effect with the 2nd effect is
just the 2nd effect
case Some(b) => g(b)
If the 1st effect is that no value is defined then there is no 3rd value as the composition of the 1st and 2nd effects is just the 1st effect
case None => None
implicit class OptionFunctionOps[A, B](f: A => Option[B]) {
def >=>[C](g: B => Option[C]): A => Option[C] =
a => f(a) match {
case Some(b) => g(b)
case None => None
}
}
Let’s now look at how the compositional responsibilities are discharged in the List monad:
1) use f to compute a first value wrapped in a functional effect
just call f – the first value consists of the B items in the resulting List[B]
f(a)
2) dig underneath the wrapper to access the first value, discarding the wrapper
digging under the wrapper and discarding it is done by foldRight, which calls its callback function with each B item in the first value
f(a).foldRight(Nil, (b:B, cs:List[C]) => concatenate(g(b), cs))
3) use g to compute, using the first value, a second value also wrapped in a functional effect
callback function g is called with each B item in the first value, so the second value consists of all List[C] results returned by g
(b:B, …) => …(g(b), …))
4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects
If the 1st effect is that there are no B items then there are no 2nd and 3rd values and the composition of 1st and 2nd effect is also that
there are no items
f(a) is Nil so f(a).foldright(…) is also Nil
otherwise the 1st effect is the multiplicity of items in the 1st value, the 2nd effect is the multiplicity of items in the 2nd value, the 3rd
value is the concatenation of all the List[C] results returned by g, and the composition of the 1st and 2nd effects is the multiplicity
of items in the concatenation
f(a).foldRight(Nil, (b:B, cs:List[C]) => concatenate(g(b), cs))
implicit class ListFunctionOps[A,B](f: A => List[B]) {
def >=>[C](g: B => List[C]): A => List[C] =
a => f(a).foldRight(Nil, (b:B, cs:List[C]) => concatenate(g(b), cs))
}
def foldRight[B](b: B,
f:(A, B) => B): B =
this match {
case Nil => b
case Cons(a, tail) =>
f(a, tail.foldRight(b, f))
}
def concatenate[A](left: List[A],
right: List[A]): List[A] =
left match {
case Nil =>
right
case Cons(head, tail) =>
Cons(head, concatenate(tail, right))
}
What we have done so far is take three monads and define them in terms of Kleisli composition and unit.
In the next three slides we are going to refactor the three monads so that they are defined in terms of
flatMap and unit and see how the compositional responsibilities get redistributed.@philip_schwarz
COMPOSITIONAL RESPONSIBILITY
1. use f to compute a first value wrapped in a functional effect
2. dig underneath the wrapper to access the first value, discarding the wrapper
3. use g to compute, using the first value, a second value also wrapped in a functional effect
4. return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects
LOCATION CHANGE
remains in >=>
moves from >=> to flatMap
moves from >=> to flatMap
moves from >=> to flatMap
Id monad defined in terms of Kleisli composition and unit Id monad defined in terms of flatMap and unit
dig underneath wrapper
invoke 2nd function
discard wrapper
invoke 1st function
compose effects
dig underneath wrapper
invoke 2nd function
discard wrapper
compose effects
invoke 1st function
Option monad defined in terms of Kleisli composition and unit Option monad defined in terms of flatMap and unit
dig underneath wrapper
invoke 2nd function
discard wrapper
invoke 1st function
compose effects
dig underneath wrapper
invoke 2nd function discard wrapper
invoke 1st function
compose effects
List monad defined in terms of Kleisli composition and unit List monad defined in terms of flatMap and unit
invoke 2nd function
invoke 1st function dig underneath wrapper discard wrappercompose effects
dig underneath wrapper
invoke 2nd function
discard wrapper
invoke 1st function
compose effects
And finally, we are going to refactor the three monads so that they are defined in terms of map,
join and unit and again see how the compositional responsibilities get redistributed.
@philip_schwarz
COMPOSITIONAL RESPONSIBILITY
1. use f to compute a first value wrapped in a functional effect
2. dig underneath the wrapper to access the first value, discarding the wrapper
3. use g to compute, using the first value, a second value also wrapped in a functional effect
4. return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects
LOCATION CHANGE
remains in >=>
moves from flatMap to map/join
moves from flatMap to map
moves from flatMap to join
Id monad defined in terms of flatMap and unit Id monad defined in terms of map, join and unit
dig underneath wrapper
invoke 2nd function
discard wrapper
compose effects
invoke 1st function
discard wrapper
compose effects
invoke 2nd function
invoke 1st function
dig underneath wrapper
Option monad defined in terms of flatMap and unit Option monad defined in terms of map, join and unit
c
discard wrapper
compose effects
dig underneath wrapper
invoke 2nd function
discard wrapper
compose effects
invoke 1st function
invoke 2nd function
dig underneath wrapper
invoke 1st function
c
List monad defined in terms of flatMap and unit List monad defined in terms of map, join and unit
dig underneath wrapper
invoke 2nd function
discard wrapper
compose effects
invoke 1st function
invoke 2nd function
dig underneath wrapper
invoke 1st function discard wrapper
compose effects
See the following slide deck for
the list of all available decks in
the MONAD FACT series
https://www.slideshare.net/pjschwarz

Weitere ähnliche Inhalte

Was ist angesagt?

Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaSierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Philip Schwarz
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 
Function Composition - forward composition versus backward composition
Function Composition - forward composition versus backward compositionFunction Composition - forward composition versus backward composition
Function Composition - forward composition versus backward compositionPhilip Schwarz
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Philip Schwarz
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Philip Schwarz
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldPhilip Schwarz
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
 
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...Philip Schwarz
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Philip Schwarz
 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsPhilip Schwarz
 

Was ist angesagt? (20)

Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaSierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
State Monad
State MonadState Monad
State Monad
 
Function Composition - forward composition versus backward composition
Function Composition - forward composition versus backward compositionFunction Composition - forward composition versus backward composition
Function Composition - forward composition versus backward composition
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
 
Functors
FunctorsFunctors
Functors
 

Ähnlich wie Monad Fact #4

Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed worldDebasish Ghosh
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsVasil Remeniuk
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to MonadsLawrence Evans
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)Eric Torreborre
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator PatternEric Torreborre
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structuresMohd Arif
 
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Philip Schwarz
 
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift7 Habits For a More Functional Swift
7 Habits For a More Functional SwiftJason Larsen
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2Hang Zhao
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystifiedAlessandro Lacava
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adtsHang Zhao
 

Ähnlich wie Monad Fact #4 (20)

Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 
Array
ArrayArray
Array
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
 
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift7 Habits For a More Functional Swift
7 Habits For a More Functional Swift
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
 
Chapter2
Chapter2Chapter2
Chapter2
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystified
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 

Mehr von Philip Schwarz

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Folding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesFolding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesPhilip Schwarz
 
Folding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a seriesFolding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a seriesPhilip Schwarz
 
Folding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first in a seriesFolding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first in a seriesPhilip Schwarz
 
Scala Left Fold Parallelisation - Three Approaches
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three ApproachesPhilip Schwarz
 
Tagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsPhilip Schwarz
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsPhilip Schwarz
 
A sighting of traverse_ function in Practical FP in Scala
A sighting of traverse_ function in Practical FP in ScalaA sighting of traverse_ function in Practical FP in Scala
A sighting of traverse_ function in Practical FP in ScalaPhilip Schwarz
 
A sighting of traverseFilter and foldMap in Practical FP in Scala
A sighting of traverseFilter and foldMap in Practical FP in ScalaA sighting of traverseFilter and foldMap in Practical FP in Scala
A sighting of traverseFilter and foldMap in Practical FP in ScalaPhilip Schwarz
 
A sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in ScalaPhilip Schwarz
 
N-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsPhilip Schwarz
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...Philip Schwarz
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an examplePhilip Schwarz
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an examplePhilip Schwarz
 
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...Philip Schwarz
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...Philip Schwarz
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...Philip Schwarz
 
Jordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axiomsJordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axiomsPhilip Schwarz
 
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Philip Schwarz
 

Mehr von Philip Schwarz (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Folding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesFolding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a series
 
Folding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a seriesFolding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a series
 
Folding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first in a seriesFolding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first in a series
 
Scala Left Fold Parallelisation - Three Approaches
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three Approaches
 
Tagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also Programs
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
 
A sighting of traverse_ function in Practical FP in Scala
A sighting of traverse_ function in Practical FP in ScalaA sighting of traverse_ function in Practical FP in Scala
A sighting of traverse_ function in Practical FP in Scala
 
A sighting of traverseFilter and foldMap in Practical FP in Scala
A sighting of traverseFilter and foldMap in Practical FP in ScalaA sighting of traverseFilter and foldMap in Practical FP in Scala
A sighting of traverseFilter and foldMap in Practical FP in Scala
 
A sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in Scala
 
N-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets Cats
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
 
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
 
Jordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axiomsJordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axioms
 
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
 

Kürzlich hochgeladen

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 

Kürzlich hochgeladen (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 

Monad Fact #4

  • 1. MONAD FACT #4 @philip_schwarzslides by https://www.slideshare.net/pjschwarz a monad is an implementation of one of the minimal sets of monadic combinators, satisfying the laws of associativity and identity see how compositional responsibilities are distributed in each combinator set
  • 2. We’ve seen three minimal sets of primitive Monad combinators, and instances of Monad will have to provide implementations of one of these sets: • unit and flatMap • unit and compose • unit, map, and join And we know that there are two monad laws to be satisfied, associativity and identity, that can be formulated in various ways. So we can state plainly what a monad is: A monad is an implementation of one of the minimal sets of monadic combinators, satisfying the laws of associativity and identity. That’s a perfectly respectable, precise, and terse definition. And if we’re being precise, this is the only correct definition. A monad is precisely defined by its operations and laws; no more, no less. Runar Bjarnason @runarorama Paul Chiusano @pchiusano Functional Programming in Scala by Paul Chiusano and Runar Bjarnason
  • 3. One of the minimal sets of primitive Monad combinators seen on the previous slide consists of a unit function and a compose function. The compose function in question is Kleisli composition. If you need an introduction to Kleisli composition then see MONAD FACT #2. If you need an introduction to the unit function then see MONAD FACT #1. Another set of combinators includes the join function. In Scala this function is known as flatten. @philip_schwarz
  • 4. Let’s take the simplest monad, i.e. the identity monad, which does nothing, and let’s define it in terms of Kleisli composition and unit. The Id monad wraps a value of some type A Id also acts as the unit function. i.e. to lift the value 3 into the Id monad we use Id(3). case class Id[A](value: A) Now we have to come up with a body for the Kleisli composition function (shown below as the infix fish operator >=>): The body must be a function of type A => Id[C] a => ??? The only way we can get an Id[C] is by calling g, which takes a B as a parameter. But all we have to work with are the a parameter, which is of type A, and function f. But that is fine because if we call f with a we get an Id[B] and if we then ask the latter for the B value that it wraps, we have the B that we need to invoke g. a => g(f(a).value) And here is a simple test for the functionSo here is how we define Kleisli composition implicit class IdFunctionOps[A,B](f: A => Id[B]) { def >=>[C](g: B => Id[C]): A => Id[C] = ??? } implicit class IdFunctionOps[A,B](f: A => Id[B]) { def >=>[C](g: B => Id[C]): A => Id[C] = a => g(f(a).value) } val double: Int => Id[Int] = n => Id(n * 2) val square: Int => Id[Int] = n => Id(n * n) assert( (double >=> square)(3) == Id(36))
  • 5. Here is a simple test for join Here is a test for our map function So yes, we have defined the identity monad in terms of Kleisli composition and unit. But we want to be able to use the monad in a for comprehension, so we now have to define a flatMap function and a map function. The flatMap function can be defined in terms of Kleisli composition: and map can then be defined in terms of flatMap: case class Id[A](value: A) object Id { implicit class IdFunctionOps[A,B](f: A => Id[B]) { def >=>[C](g: B => Id[C]): A => Id[C] = a => g(f(a).value) } } case class Id[A](value: A) { def flatMap[B](f: A => Id[B]): Id[B] = (((_:Unit) => this) >=> f)(()) } case class Id[A](value: A) { def flatMap[B](f: A => Id[B]): Id[B] = (((_:Unit) => this) >=> f)(()) def map[B](f: A => B): Id[B] = this flatMap { a => Id(f(a)) } } val increment: Int => Int = n => n + 1 assert( (Id(3) map increment) == Id(4) ) And here is a test for both our map and flatMap functions val result = for { six <- double(3) thirtySix <- square(six) } yield six + thirtySix assert(result == Id(42)) We can also define join (aka flatten) in terms of the flatMap function def join[A](mma: Id[Id[A]]): Id[A] = mma flatMap identity assert( join(Id(Id(3))) == Id(3) )
  • 6. Here is the whole code for the identity monad In this slide deck we are going to compare the identity monad with the Option monad and the List monad. How do the functions of the above identity monad, which is defined in terms of Kleisli composition, relate to the equivalent Option monad functions? See the next slide for the differences. case class Id[A](value: A) { def flatMap[B](f: A => Id[B]): Id[B] = (((_:Unit) => this) >=> f)(()) def map[B](f: A => B): Id[B] = this flatMap { a => Id(f(a)) } } object Id { implicit class IdFunctionOps[A,B](f: A => Id[B]) { def >=>[C](g: B => Id[C]): A => Id[C] = a => g(f(a).value) } def join[A](mma: Id[Id[A]]): Id[A] = mma flatMap identity } val double: Int => Id[Int] = n => Id(n * 2) val square: Int => Id[Int] = n => Id(n * n) assert( (double >=> square)(3) == Id(36)) val increment: Int => Int = n => n + 1 assert( (Id(3) map increment) == Id(4) ) assert( join(Id(Id(3))) == Id(3) ) val result = for { six <- double(3) thirtySix <- square(six) } yield six + thirtySix assert(result == Id(42))
  • 7. First of all we see that apart from the obvious swapping of Id for Option in their signatures, the flatMap and join functions of the two monads are identical. The only difference between the map functions of the two monads are the unit functions that they use: one uses Id and the other uses Some. So the only real difference between the two monads is the logic in the fish operator. That makes sense, since the monads are defined in terms of unit and Kleisli composition, and since unit is a very simple function. @philip_schwarz
  • 8. The same is true of the differences between the functions of the Id monad and those of the List monad: the differences are in the fish operator; The apparent additional difference between the map functions is only due to the fact that we are using Cons(x,Nil) as a unit function rather List(x), i.e. some singleton list constructor that we could define.
  • 9. Let’s now turn to the function that differentiates the monads, i.e. Kleisli composition (the fish operator) The composite function that it returns (the composition of f and g) has the following responsibilities (let’s call them compositional responsibilities): 1) use f to compute a first value wrapped in a functional effect 2) dig underneath the wrapper to access the first value, discarding the wrapper 3) Use g to compute, using the first value, a second value also wrapped in a functional effect 4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects As a slight variation on that, we can replace ‘wrapped in’ with ‘in the context of’ 1) use f to compute a first value in the context of a functional effect 2) dig inside the context to access the first value, discarding the context 3) Use g to compute, using the first value, a second value also in the context of a functional effect 4) return a third value in the context of a functional effect that represents the composition (combination) of the first two functional effects implicit class IdFunctionOps[A,B](f: A => Id[B]) { def >=>[C](g: B => Id[C]): A => Id[C] = a => ??? }
  • 10. Here are the Kleisli composition functions of the three monads (their fish operators). Notice how different they are. The one in the identity monad seems to do almost nothing, the one in the Option monad seems to do a bit more work, and the one in the List monad does quite a bit more. See the next slide for some test code for the Option monad and the List monad. object Option { implicit class OptionFunctionOps[A, B](f: A => Option[B]) { def >=>[C](g: B => Option[C]): A => Option[C] = a => f(a) match { case Some(b) => g(b) case None => None } } } object Id { implicit class IdFunctionOps[A,B](f: A => Id[B]) { def >=>[C](g: B => Id[C]): A => Id[C] = a => g(f(a).value) } } sealed trait List[+A] { def foldRight[B](b: B, f: (A,B) => B): B = this match { case Nil => b case Cons(a, tail) => f(a, tail.foldRight(zero, f)) } } object List { implicit class ListFunctionOps[A,B](f: A => List[B]) { def >=>[C](g: B => List[C]): A => List[C] = a => f(a).foldRight(Nil, (b:B, cs:List[C]) => concatenate(g(b), cs)) } def concatenate[A](left: List[A], right: List[A]): List[A] = left match { case Nil => right case Cons(head, tail) => Cons(head, concatenate(tail, right)) } }
  • 11. // Tests for Option monad assert( join(Some(Some(3))) == Some(3) ) val increment: Int => Int = n => n + 1 assert( (Some(3) map increment) == Some(4) ) val double: Int => Option[Int] = n => if (n % 2 == 1) Some(n * 2) else None val square: Int => Option[Int] = n => if (n < 100) Some(n * n) else None assert( (double >=> square)(3) == Some(36)) val result = for { six <- double(3) thirtySix <- square(six) } yield six + thirtySix assert(result == Some(42)) // Tests for List monad assert( join(Cons( Cons(1, Cons(2, Nil)), Cons( Cons(3, Cons(4, Nil)), Nil)) ) == Cons(1, Cons(2, Cons(3, Cons(4, Nil))) ) ) val increment: Int => Int = n => n + 1 assert( (Cons(1, Cons(2, Cons(3, Cons(4, Nil))) ) map increment) == Cons(2, Cons(3, Cons(4, Cons(5, Nil))) ) ) val double: Int => List[Int] = n => Cons(n, Cons(n * 2, Nil)) val square: Int => List[Int] = n => Cons(n, Cons(n * n, Nil)) assert( (double >=> square)(3) == Cons(3,Cons(9,Cons(6,Cons(36, Nil))))) val result = for { x <- double(3) y <- square(x) } yield Cons(x, Cons(y, Nil)) assert(result == Cons( Cons(3,Cons(3,Nil)), Cons( Cons(3,Cons(9,Nil)), Cons( Cons(6,Cons(6,Nil)), Cons( Cons(6,Cons(36,Nil)), Nil)))))
  • 12. Next, we are going to look at the Kleisli composition functions of Id, Option and List to see how each of them discharges its compositional responsibilities. @philip_schwarz
  • 13. In the special case of the Identity monad, which does nothing, the compositional responsibilities are discharged in a degenerate and curious way: 1) use f to compute a first value wrapped in a functional effect just call f f(a) 2) dig underneath the wrapper to access the first value, discarding the wrapper digging under the wrapper simply amounts to asking the resulting Id[B] for the B that it is wrapping f(a).value 3) use g to compute, using the first value, a second value also wrapped in a functional effect just call g with the first value g(f(a).value) 4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects because the effect of the Id monad is nonexistent, there simply is nothing to combine, so just return the second value g(f(a).value) implicit class IdFunctionOps[A,B](f: A => Id[B]) { def >=>[C](g: B => Id[C]): A => Id[C] = a => g(f(a).value) }
  • 14. Next, let’s look at how the compositional responsibilities are discharged in the Option monad: 1) use f to compute a first value wrapped in a functional effect just call f f(a) 2) dig underneath the wrapper to access the first value, discarding the wrapper digging under the wrapper and discarding it is done by pattern matching, destructuring Option[B] to get the wrapped B value Some(b) 3) use g to compute, using the first value, a second value also wrapped in a functional effect just call g with the first value g(b) 4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects If the 1st effect is that a value is defined then the 3rd value is just the 2nd value and composition of the 1st effect with the 2nd effect is just the 2nd effect case Some(b) => g(b) If the 1st effect is that no value is defined then there is no 3rd value as the composition of the 1st and 2nd effects is just the 1st effect case None => None implicit class OptionFunctionOps[A, B](f: A => Option[B]) { def >=>[C](g: B => Option[C]): A => Option[C] = a => f(a) match { case Some(b) => g(b) case None => None } }
  • 15. Let’s now look at how the compositional responsibilities are discharged in the List monad: 1) use f to compute a first value wrapped in a functional effect just call f – the first value consists of the B items in the resulting List[B] f(a) 2) dig underneath the wrapper to access the first value, discarding the wrapper digging under the wrapper and discarding it is done by foldRight, which calls its callback function with each B item in the first value f(a).foldRight(Nil, (b:B, cs:List[C]) => concatenate(g(b), cs)) 3) use g to compute, using the first value, a second value also wrapped in a functional effect callback function g is called with each B item in the first value, so the second value consists of all List[C] results returned by g (b:B, …) => …(g(b), …)) 4) return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects If the 1st effect is that there are no B items then there are no 2nd and 3rd values and the composition of 1st and 2nd effect is also that there are no items f(a) is Nil so f(a).foldright(…) is also Nil otherwise the 1st effect is the multiplicity of items in the 1st value, the 2nd effect is the multiplicity of items in the 2nd value, the 3rd value is the concatenation of all the List[C] results returned by g, and the composition of the 1st and 2nd effects is the multiplicity of items in the concatenation f(a).foldRight(Nil, (b:B, cs:List[C]) => concatenate(g(b), cs)) implicit class ListFunctionOps[A,B](f: A => List[B]) { def >=>[C](g: B => List[C]): A => List[C] = a => f(a).foldRight(Nil, (b:B, cs:List[C]) => concatenate(g(b), cs)) } def foldRight[B](b: B, f:(A, B) => B): B = this match { case Nil => b case Cons(a, tail) => f(a, tail.foldRight(b, f)) } def concatenate[A](left: List[A], right: List[A]): List[A] = left match { case Nil => right case Cons(head, tail) => Cons(head, concatenate(tail, right)) }
  • 16. What we have done so far is take three monads and define them in terms of Kleisli composition and unit. In the next three slides we are going to refactor the three monads so that they are defined in terms of flatMap and unit and see how the compositional responsibilities get redistributed.@philip_schwarz
  • 17. COMPOSITIONAL RESPONSIBILITY 1. use f to compute a first value wrapped in a functional effect 2. dig underneath the wrapper to access the first value, discarding the wrapper 3. use g to compute, using the first value, a second value also wrapped in a functional effect 4. return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects LOCATION CHANGE remains in >=> moves from >=> to flatMap moves from >=> to flatMap moves from >=> to flatMap Id monad defined in terms of Kleisli composition and unit Id monad defined in terms of flatMap and unit dig underneath wrapper invoke 2nd function discard wrapper invoke 1st function compose effects dig underneath wrapper invoke 2nd function discard wrapper compose effects invoke 1st function
  • 18. Option monad defined in terms of Kleisli composition and unit Option monad defined in terms of flatMap and unit dig underneath wrapper invoke 2nd function discard wrapper invoke 1st function compose effects dig underneath wrapper invoke 2nd function discard wrapper invoke 1st function compose effects
  • 19. List monad defined in terms of Kleisli composition and unit List monad defined in terms of flatMap and unit invoke 2nd function invoke 1st function dig underneath wrapper discard wrappercompose effects dig underneath wrapper invoke 2nd function discard wrapper invoke 1st function compose effects
  • 20. And finally, we are going to refactor the three monads so that they are defined in terms of map, join and unit and again see how the compositional responsibilities get redistributed. @philip_schwarz
  • 21. COMPOSITIONAL RESPONSIBILITY 1. use f to compute a first value wrapped in a functional effect 2. dig underneath the wrapper to access the first value, discarding the wrapper 3. use g to compute, using the first value, a second value also wrapped in a functional effect 4. return a third value wrapped in a functional effect that represents the composition (combination) of the first two functional effects LOCATION CHANGE remains in >=> moves from flatMap to map/join moves from flatMap to map moves from flatMap to join Id monad defined in terms of flatMap and unit Id monad defined in terms of map, join and unit dig underneath wrapper invoke 2nd function discard wrapper compose effects invoke 1st function discard wrapper compose effects invoke 2nd function invoke 1st function dig underneath wrapper
  • 22. Option monad defined in terms of flatMap and unit Option monad defined in terms of map, join and unit c discard wrapper compose effects dig underneath wrapper invoke 2nd function discard wrapper compose effects invoke 1st function invoke 2nd function dig underneath wrapper invoke 1st function
  • 23. c List monad defined in terms of flatMap and unit List monad defined in terms of map, join and unit dig underneath wrapper invoke 2nd function discard wrapper compose effects invoke 1st function invoke 2nd function dig underneath wrapper invoke 1st function discard wrapper compose effects
  • 24. See the following slide deck for the list of all available decks in the MONAD FACT series https://www.slideshare.net/pjschwarz