SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Functor	Composition
@BartoszMilewski
@philip_schwarzslides	by
including	(starting	from)	the	definition	in
@philip_schwarz
Let’s look at Bartosz Milewski’s explanation of the fact that Functors compose.
If you need an introduction to Functors then see the following
https://www.slideshare.net/pjschwarz/functors
@philip_schwarz
https://www.slideshare.net/pjschwarz/functor-laws
Scala	Edition	Contains	code	
snippets	in	Haskell and	Scala
7.1	Functors	in	Programming
7.1.1	The	Option	Functor
def map[A, B](f: A => B)(fa: Option[A]): Option[B]
def map[A, B](f: A => B): Option[A] => Option[B] = {
case None => None
case Some(x) => Some(f(x))
}
7.1.4	Typeclasses
trait Functor[F[_]] {
def map[A, B](f: A => B)(fa: F[A]): F[B]
}
implicit val optionFunctor = new Functor[Option] {
def map[A, B](f: A => B)(fa: Option[A]): Option[B] =
fa match {
case None => None
case Some(x) => Some(f(x))
}
}
7.1.6	The	List	Functor
implicit val listFunctor = new Functor[List] {
def map[A, B](f: A => B)(fa: List[A]): List[B] =
fa match {
case Nil => Nil
case x :: t => f(x) :: map(f)(t)
}
}
It’s not hard to convince yourself that functors between categories
compose, just like functions between sets compose. A composition
of two functors, when acting on objects, is just the composition of
their respective object mappings; and similarly when acting on
morphisms. After jumping through two functors, identity morphisms
end up as identity morphisms, and compositions of morphisms finish
up as compositions of morphisms. There’s really nothing much to it.
In particular, it’s easy to compose endofunctors. Remember the
function maybeTail? I’ll rewrite it using Scala’s built in
implementation of lists:
7.3	Functor	Composition
def maybeTail[A]: List[A] => Option[List[A]] = {
case Nil => None
case x :: xs => Some(xs)
}
The result of maybeTail is of a type that’s a composition of two functors, Option and List, acting on A.
Each of these functors is equipped with its own version of map, but what if we want to apply some
function f to the contents of the composite: an Option of a List? We have to break through two layers of
functors. We can use map to break through the outer Option. But we can’t just send f inside Option
because f doesn’t work on lists. We have to send (map f) to operate on the inner list. For instance, let’s
see how we can square the elements of an Option of a List of integers:
def square: Int => Int = x => x * x
val maybeList: Option[List[Int]] = Some(1 :: (2 :: (3 :: Nil)))
val maybeListSquared = (optionFunctor map (listFunctor map square))(maybeList)
assert( maybeListSquared == Some(1 :: (4 :: (9 :: Nil))) )
The compiler, after analyzing the types, will figure out that, for the outer map, it should use the
implementation from the Option instance, and for the inner one, the List functor implementation.
def square: Int => Int = x => x * x
val maybeList: Option[List[Int]] = Some(1 :: (2 :: (3 :: Nil)))
val maybeListSquared = (optionFunctor map (listFunctor map square))(maybeList)
It	may	not	be	immediately	obvious	that	the	last	line	in	the	above	code	may	be	rewritten	as:
def mapOption[A, B]: (A => B) => Option[A] => Option[B] = optionFunctor.map
def mapList[A, B]: (A => B) => List[A] => List[B] = listFunctor.map
def mapOptionList[A, B]: (A => B) => Option[List[A]] => Option[List[B]] =
mapOption compose mapList
val maybeListSquared = mapOptionList(square)(maybeList)
But	remember	that	map	may	be	considered	a	function	of	just	one	argument:
def map[F[_], A, B]: (A => B) => (F[A] => F[B])
In	our	case,	the	second	map in	(mapOption compose mapList)	takes	as	its	argument:
def square: Int => Int
and	returns	a	function	of	the	type:
List[Int] => List[Int]
The	first	map then	takes	that	function	and	returns	a	function:
Option[List[Int]] => Option[List[Int]]
Finally,	that	function	is	applied	to	maybeList.	So	the	composition of	two	functors is	a functor	whose	map is	
the	composition of	the	corresponding	maps.
Scala	Edition Contains	code	
snippets	in	Haskell and	Scala
Yes, to aid comprehension in our context, I have taken the liberty to
rename a few things, e.g. fmap à map, Maybe à Option, a à A, Haskell
à Scala, mis à maybeList, mis2 à maybeListSquared, Cons à ::
@BartoszMilewski
In	the	next	two	slides	I	have	a	go	at	visualizing	
this	notion	of	a	composite Functor.
@philip_schwarz
A B
f
F[A] F[B]
f↑F
G[A] G[B]
f↑G
F[G[A]] F[G[B]]
(f↑G) ↑F
mapG
mapF
--
- -
(mapF compose	mapG)
mapF lifts	function	f into	F
f↑F		is	f lifted	into	F
Int Int
square
Option[Int]
square↑Option
List[Int] List[Int]
square↑List
Option[List[Int]] Option[List[Int]]
mapListmapOption
--
- .
(mapOption compose	mapList)
(square↑List)↑Option
Option[Int]
def square: Int => Int = x => x * x
def mapOption[A,B]:
(A => B) => Option[A] => Option[B] =
optionFunctor.map
def mapList[A,B]:
(A => B) => List[A] => List[B] =
listFunctor.map
def mapOptionList[A,B]:
(A => B) => Option[List[A]] => Option[List[B]] =
mapOption compose mapList
package scala
trait Function1 …
/** Composes two instances of Function1 in a new
* Function1, with this function applied last.
* @tparam A the type to which function
* `g` can be applied
* @param g a function A => T1
* @return a new function `f` such that
* `f(x) == apply(g(x))`
*/
…def compose[A](g: A => T1): A => R =
{ x => apply(g(x)) }
…
mapOption = mapOption
mapList = mapList
(mapOption compose mapList) = mapOptionList
the	composition of	two	functors is	a	functor whose	map is	the	composition of	the	corresponding	maps
mapF lifts	function	f into	F
f↑F		is	f lifted	into	F
Int Int
square
Option[Int]
square↑Option
List[Int] List[Int]
square↑List
Option[List[Int]] Option[List[Int]]
mapListmapOption
--
- .
(mapOption compose	mapList)
(square↑List)↑Option
Option[Int]
mapOption = mapOption
mapList = mapList
(mapOption compose mapList) = mapOptionList
def square: Int => Int = x => x * x
def mapOption[A,B]: (A => B) => Option[A] => Option[B] = optionFunctor.map
def mapList[A,B]: (A => B) => List[A] => List[B] = listFunctor.map
def mapOptionList[A,B]: (A => B) => Option[List[A]] => Option[List[B]] = mapOption compose mapList
mapping f with the composition of two functors is
the same as first mapping f with the 1st functor and
then mapping the result with the 2nd functor.
The	composition of	two	functors is	a	functor whose	map is	the	composition of	the	corresponding	maps
// mapOptionList = mapOption compose mapList
assert(mapOptionList(square)(Some(List(1,2,3))) == mapOption(mapList(square))(Some(List(1,2,3))))
assert(mapOptionList(square)(Some(List(1,2,3))) == Some(List(1,4,9)))
assert((mapOption(mapList(square))(Some(List(1,2,3))) == Some(List(1,4,9)))
@philip_schwarz
To get a fuller picture of the notion of a composite Functor, let’s look at
Functor laws.
See the following for a more comprehensive introduction to Functor laws.
@philip_schwarz
https://www.slideshare.net/pjschwarz/functor-laws
A B
C
f
g
g	∘ f
idB
idB	∘ 	f
F[A] F[B]
F[C]
f↑F
g↑F
g↑F ∘ f↑F
idB↑F
f↑F		∘		idB↑F	
C
D
h
idC
h	∘ idC
F[C]
F[D]
h↑F
idC↑F
idC↑F	∘	h↑F	
C1 C2
C1	=	C2	=	Scala	types	and	functions
• objects:	types
• arrows:	functions
• composition	operation:		compose function,	denoted	here	by	∘
• identity	arrows:	identity	function	T	=>	T,	denoted	here	by	idT
A	functor	F from	C1	to	C2	consisting	of	
• a	type	constructor	F that	maps	type	A	to	F[A]	
• a	map function	from	function	f:A=>B	to	function	f↑F	:F[A]	=>	F[B]	
Functor	Laws
F(g	∘	f)	=	F(g)	∘	F(f)				i.e. map(g	∘	f)	=	map(g)	∘	map(f)	
F(idX)	=	idF(X)	 i.e. map(idX)	=	idX↑	F	
F[B]B
f↑F		is	function	f	lifted	into	context	F
F[A] is	type	A	lifted	into	context	F
idX↑	F	is	idX lifted	into	context	F
F
F
F
F(g	∘	f)	=	F(g)	∘	F(f)
map(g	∘	f)	=	map(g)	∘	map(f)
F(A)	=	F[A]
F(B)	=	F[B]
F(C)	=	F[C]
F(f:A=>B)	= map(f)	=	f↑F:F[A]=>F[B]
F(g:B=>C)	= map(g)	=	g↑F:F[B]=>F[C]
F(g∘f:A=>C)	= map(g∘f)	=	g↑F∘f↑F:F[A]=>F[C]
the	mapping of	the	composition is	
the	composition of	the	mappings
A	Functor	from	the	category	of	‘Scala types	and	functions’	to	itself
A B
C
f
g
g	∘ f
idB
idB	∘ f = f
F[A] F[B]
F[C]
f↑F
g↑F
g↑F ∘ f↑F
idB↑F
idB↑F	∘ f↑F =	f↑F
C
D
h
idC
h	∘ idC
F[C]
F[D]
h↑F
idC↑F
h↑F∘idC↑F	=	h↑F	
C1 C2 F[B]B
F
F
F
F(idX)	=	idF(X)
map(idX)	=	idX↑F	
the	mapping	of	an	arrow’s	identity	is	
the	identity of	the	arrow’s	mapping
F
F
F
F(A)	=	F[A]
F(B)	=	F[B]
F(C)	=	F[N]
F(D)	=	F[P]
F(f)	=	f↑F
F(h)	=	h↑F
F(idB)	=	idB↑F
F(idC)	=	idC↑F
Now let’s revisit that with the notion of a
composite Functor in mind.
@philip_schwarz
A B
C
f
g
g	∘ f
F[A] F[B]
F[C]
f↑F
g↑F
g↑F ∘ f↑F
G[F[A]] G[F[B]]
G[F[C]]
(f↑F)↑G
(g↑F)↑G
(g↑F)↑G ∘ (f↑F)↑G
Functor	F
the	mapping of	the	composition is	
the	composition of	the	mappings
The	composition of	Functors G	and	F	is	a	composite functor G	∘	F from	C1	to	C2	consisting	of	
• a	type	constructor
• a	mapG∘F function	from	function	f:A=>B	to	function	(f↑F)↑G:	G[F[A]]	=>	G[F[B]]	
where	mapG∘F =	mapG ∘ mapF
G∘F(idX)	=	idG∘F(X)	
i.e.	mapG∘F(idX)	=	(idX↑	F)↑	G	
the	mapping	of	an	arrow’s	identity	is	
the	identity of	the	arrow’s	mapping
G∘F(g	∘	f)		=		(G∘F(g))	∘ 	(G∘F(f))	
i.e.	mapG∘F(g	∘	f)		=		(mapG∘F	(g))	∘	(mapG∘F	(f))
Here	is	what	the	Functor Laws look	like	for	a	composite Functor
To	keep	the	diagram	simple,	we	are	only	illustrating	the	first	law	
and	we	are	not	showing	the	Functor	mappings	between	
functions,	e.g.						f								F f↑F	 G (f↑F)↑G
Functions:	f,	g,	g	∘ f,	f↑F,	g↑F,	g↑F∘f↑F,	etc
Higher	Order	functions:	mapF,	mapG,	mapG∘F
mapF
mapG
// mapOptionList = mapOption compose mapList
assert(((mapOptionList(inc compose twice))(Some(List(1,2,3))))
== (mapOption(mapList(inc)))((mapOption(mapList(twice)))(Some(List(1,2,3)))))
assert((mapOption(mapList(inc)))((mapOption(mapList(twice)))(Some(List(1,2,3)))) == Some(List(3,5,7)))
assert(((mapOptionList(inc compose twice))(Some(List(1,2,3)))) == Some(List(3,5,7)))
// mapOptionList = mapOption compose mapList
assert(mapOptionList(square)(Some(List(1,2,3))) == mapOption(mapList(square))(Some(List(1,2,3))))
assert(mapOptionList(square)(Some(List(1,2,3))) == Some(List(1,4,9)))
assert((mapOption(mapList(square))(Some(List(1,2,3))) == Some(List(1,4,9)))
Mapping the composition of g and f
with the composition of two functors is
the same as doing the following:
1. mapping f	first	with	the	1st functor
and	then	with	the	2nd functor
2. mapping g	first	with	the	1st functor
and	then	with	the	2nd functor
3. applying	first	the	function	computed	
in	(1)	and	then	the	function	
computed	in	(2)
Mapping f with the composition of two
functors is the same as first mapping f
with the 1st functor and then mapping
the result with the 2nd functor.
assert( (mapList(inc compose twice))(List(1,2,3)) == (mapList(inc))(mapList(twice)(List(1,2,3))) )
assert( (mapList(inc compose twice))(List(1,2,3)) == List(3,5,7) )
assert( (mapList(inc))(mapList(twice)(List(1,2,3))) == List(3,5,7) )
Mapping the composition of f and g is
the same as first applying the mapping
of f and then applying the mapping of g.
1st Functor	Law
1st Functor	Law	and	Functor	Composition	together
Functor	Composition
The	composition of	two	functors is	a	functor whose	map is	the	composition of	the	corresponding	maps
def inc: Int => Int = _ + 1
def twice: Int => Int = _ * 2
package scalaz
private trait CompositionFunctor[F[_], G[_]] extends Functor[λ[α => F[G[α]]]] {
implicit def F: Functor[F]
implicit def G: Functor[G]
override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] =
F(fga)(G.lift(f))
}
In Scalaz there is a CompositionFunctor trait
Let’s adopt a similar approach to provide a functor composition trait
trait CompositionFunctor[F[_],G[_]] extends Functor[λ[α => F[G[α]]]] {
implicit def F: Functor[F]
implicit def G: Functor[G]
override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] =
F.map(fga)(ga => G.map(ga)(f))
}
Let’s see two examples of composing functors
using our CompositionFunctor
@philip_schwarz
implicit val listFunctor = new Functor[List] {
def map[A, B](fa: List[A])(f: A => B): List[B] = fa map f
}
implicit val optionFunctor = new Functor[Option] {
def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa map f
}
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
trait CompositionFunctor[F[_],G[_]] extends Functor[λ[α => F[G[α]]]] {
implicit def F: Functor[F]
implicit def G: Functor[G]
override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] =
F.map(fga)(ga => G.map(ga)(f))
}
val listOptionFunctor = new CompositionFunctor[List,Option] {
implicit def F: Functor[List] = listFunctor
implicit def G: Functor[Option] = optionFunctor
}
val twice: Int => Int = _ * 2
val listOfOption = List(Some(3),None,Some(4))
val doubledListOfOption = List(Some(6),None,Some(8))
assert(listOptionFunctor.map(listOfOption)(twice) == doubledListOfOption)
using	https://github.com/non/kind-projector	
allows	us	to	simplify	type	lambda	({type f[α] = F[G[α]]})#f
to	this:	λ[α => F[G[α]]]
Composing the	List Functor
with	the	Option Functor.
implicit val listFunctor = new Functor[List] {
def map[A, B](fa: List[A])(f: A => B): List[B] = fa map f
}
import scala.concurrent.ExecutionContext.Implicits.global
val futureFunctor = new Functor[Future] {
def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa map f (global)
}
val futureOptionFunctor = new CompositionFunctor[Future,Option] {
implicit def F: Functor[Future] = futureFunctor
implicit def G: Functor[Option] = optionFunctor
}
assert( Await.result( futureOptionFunctor.map(Future{ Some(3) })(twice), Duration.Inf) == Some(6) )
Composing the	Option	Functor
with	the	Future	Functor.	
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
trait CompositionFunctor[F[_],G[_]] extends Functor[λ[α => F[G[α]]]] {
implicit def F: Functor[F]
implicit def G: Functor[G]
override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] =
F.map(fga)(ga => G.map(ga)(f))
}
Instead of having a trait, let’s have a
compose method on Functor
@philip_schwarz
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
def compose[G[_]](G:Functor[G]):Functor[λ[α=>F[G[α]]]] = {
val self = this
new Functor[λ[α => F[G[α]]]] {
override def map[A, B](fga:F[G[A]])(f:A=>B):F[G[B]] =
self.map(fga)(ga => G.map(ga)(f))
}
}
}
// Functor[List[Option]] = Functor[List] compose Functor[Option]
val optionListFunctor = listFunctor compose optionFunctor
assert(optionListFunctor.map(List(Some(1),Some(2),Some(3)))(double) == List(Some(2),Some(4),Some(6)))
// Functor[Option[List]] = Functor[Option] compose Functor[List]
val listOptionFunctor = optionFunctor compose listFunctor
assert(listOptionFunctor.map(Some(List(1,2,3)))(double) == Some(List(2,4,6)))
val double: Int => Int = _ * 2
implicit val listFunctor = new Functor[List] {
def map[A, B](fa: List[A])(f: A => B): List[B] = fa map f
}
implicit val optionFunctor = new Functor[Option] {
def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa map f
}
So	that	we	van	create	a	composite functor with	
listFunctor compose optionFunctor
rather	than	with	
new CompositionFunctor[Future[Option]]{
…
…
}

Contenu connexe

Tendances

Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Philip Schwarz
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Philip Schwarz
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1Hang Zhao
 
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Philip Schwarz
 
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
 
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
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adtsHang Zhao
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
 
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
 
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
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scalaPiotr Paradziński
 
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
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersPiotr Paradziński
 
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
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
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
 

Tendances (20)

Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
 
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'
 
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
 
Monad Fact #2
Monad Fact #2Monad Fact #2
Monad Fact #2
 
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
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
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...
 
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
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in 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...
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Why functional programming and category theory strongly matters
Why functional programming and category theory strongly mattersWhy functional programming and category theory strongly matters
Why functional programming and category theory strongly matters
 
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
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
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 ...
 

Similaire à Functor Composition

Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Philip Schwarz
 
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
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystifiedAlessandro Lacava
 
From Function1#apply to Kleisli
From Function1#apply to KleisliFrom Function1#apply to Kleisli
From Function1#apply to KleisliHermann Hueck
 
From Functor Composition to Monad Transformers
From Functor Composition to Monad TransformersFrom Functor Composition to Monad Transformers
From Functor Composition to Monad TransformersHermann Hueck
 
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
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a categorysamthemonad
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator PatternEric Torreborre
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
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
 

Similaire à Functor Composition (20)

Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...
 
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...
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystified
 
From Function1#apply to Kleisli
From Function1#apply to KleisliFrom Function1#apply to Kleisli
From Function1#apply to Kleisli
 
purrr.pdf
purrr.pdfpurrr.pdf
purrr.pdf
 
From Functor Composition to Monad Transformers
From Functor Composition to Monad TransformersFrom Functor Composition to Monad Transformers
From Functor Composition to Monad Transformers
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 
(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
 
Practical cats
Practical catsPractical cats
Practical cats
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
Monad presentation scala as a category
Monad presentation   scala as a categoryMonad presentation   scala as a category
Monad presentation scala as a category
 
08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
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)
 
Chapter7
Chapter7Chapter7
Chapter7
 
Thesis
ThesisThesis
Thesis
 
Thesis PPT
Thesis PPTThesis PPT
Thesis PPT
 

Plus de 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
 

Plus de 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...
 

Dernier

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
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
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
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
 
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
 
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
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
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
 
%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
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 

Dernier (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
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
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
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 🔝✔️✔️
 
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
 
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
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
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-...
 
%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
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Functor Composition

  • 2. @philip_schwarz Let’s look at Bartosz Milewski’s explanation of the fact that Functors compose. If you need an introduction to Functors then see the following https://www.slideshare.net/pjschwarz/functors @philip_schwarz https://www.slideshare.net/pjschwarz/functor-laws
  • 3. Scala Edition Contains code snippets in Haskell and Scala 7.1 Functors in Programming 7.1.1 The Option Functor def map[A, B](f: A => B)(fa: Option[A]): Option[B] def map[A, B](f: A => B): Option[A] => Option[B] = { case None => None case Some(x) => Some(f(x)) } 7.1.4 Typeclasses trait Functor[F[_]] { def map[A, B](f: A => B)(fa: F[A]): F[B] } implicit val optionFunctor = new Functor[Option] { def map[A, B](f: A => B)(fa: Option[A]): Option[B] = fa match { case None => None case Some(x) => Some(f(x)) } } 7.1.6 The List Functor implicit val listFunctor = new Functor[List] { def map[A, B](f: A => B)(fa: List[A]): List[B] = fa match { case Nil => Nil case x :: t => f(x) :: map(f)(t) } } It’s not hard to convince yourself that functors between categories compose, just like functions between sets compose. A composition of two functors, when acting on objects, is just the composition of their respective object mappings; and similarly when acting on morphisms. After jumping through two functors, identity morphisms end up as identity morphisms, and compositions of morphisms finish up as compositions of morphisms. There’s really nothing much to it. In particular, it’s easy to compose endofunctors. Remember the function maybeTail? I’ll rewrite it using Scala’s built in implementation of lists: 7.3 Functor Composition def maybeTail[A]: List[A] => Option[List[A]] = { case Nil => None case x :: xs => Some(xs) } The result of maybeTail is of a type that’s a composition of two functors, Option and List, acting on A. Each of these functors is equipped with its own version of map, but what if we want to apply some function f to the contents of the composite: an Option of a List? We have to break through two layers of functors. We can use map to break through the outer Option. But we can’t just send f inside Option because f doesn’t work on lists. We have to send (map f) to operate on the inner list. For instance, let’s see how we can square the elements of an Option of a List of integers: def square: Int => Int = x => x * x val maybeList: Option[List[Int]] = Some(1 :: (2 :: (3 :: Nil))) val maybeListSquared = (optionFunctor map (listFunctor map square))(maybeList) assert( maybeListSquared == Some(1 :: (4 :: (9 :: Nil))) ) The compiler, after analyzing the types, will figure out that, for the outer map, it should use the implementation from the Option instance, and for the inner one, the List functor implementation.
  • 4. def square: Int => Int = x => x * x val maybeList: Option[List[Int]] = Some(1 :: (2 :: (3 :: Nil))) val maybeListSquared = (optionFunctor map (listFunctor map square))(maybeList) It may not be immediately obvious that the last line in the above code may be rewritten as: def mapOption[A, B]: (A => B) => Option[A] => Option[B] = optionFunctor.map def mapList[A, B]: (A => B) => List[A] => List[B] = listFunctor.map def mapOptionList[A, B]: (A => B) => Option[List[A]] => Option[List[B]] = mapOption compose mapList val maybeListSquared = mapOptionList(square)(maybeList) But remember that map may be considered a function of just one argument: def map[F[_], A, B]: (A => B) => (F[A] => F[B]) In our case, the second map in (mapOption compose mapList) takes as its argument: def square: Int => Int and returns a function of the type: List[Int] => List[Int] The first map then takes that function and returns a function: Option[List[Int]] => Option[List[Int]] Finally, that function is applied to maybeList. So the composition of two functors is a functor whose map is the composition of the corresponding maps. Scala Edition Contains code snippets in Haskell and Scala Yes, to aid comprehension in our context, I have taken the liberty to rename a few things, e.g. fmap à map, Maybe à Option, a à A, Haskell à Scala, mis à maybeList, mis2 à maybeListSquared, Cons à :: @BartoszMilewski
  • 6. A B f F[A] F[B] f↑F G[A] G[B] f↑G F[G[A]] F[G[B]] (f↑G) ↑F mapG mapF -- - - (mapF compose mapG) mapF lifts function f into F f↑F is f lifted into F Int Int square Option[Int] square↑Option List[Int] List[Int] square↑List Option[List[Int]] Option[List[Int]] mapListmapOption -- - . (mapOption compose mapList) (square↑List)↑Option Option[Int] def square: Int => Int = x => x * x def mapOption[A,B]: (A => B) => Option[A] => Option[B] = optionFunctor.map def mapList[A,B]: (A => B) => List[A] => List[B] = listFunctor.map def mapOptionList[A,B]: (A => B) => Option[List[A]] => Option[List[B]] = mapOption compose mapList package scala trait Function1 … /** Composes two instances of Function1 in a new * Function1, with this function applied last. * @tparam A the type to which function * `g` can be applied * @param g a function A => T1 * @return a new function `f` such that * `f(x) == apply(g(x))` */ …def compose[A](g: A => T1): A => R = { x => apply(g(x)) } … mapOption = mapOption mapList = mapList (mapOption compose mapList) = mapOptionList the composition of two functors is a functor whose map is the composition of the corresponding maps
  • 7. mapF lifts function f into F f↑F is f lifted into F Int Int square Option[Int] square↑Option List[Int] List[Int] square↑List Option[List[Int]] Option[List[Int]] mapListmapOption -- - . (mapOption compose mapList) (square↑List)↑Option Option[Int] mapOption = mapOption mapList = mapList (mapOption compose mapList) = mapOptionList def square: Int => Int = x => x * x def mapOption[A,B]: (A => B) => Option[A] => Option[B] = optionFunctor.map def mapList[A,B]: (A => B) => List[A] => List[B] = listFunctor.map def mapOptionList[A,B]: (A => B) => Option[List[A]] => Option[List[B]] = mapOption compose mapList mapping f with the composition of two functors is the same as first mapping f with the 1st functor and then mapping the result with the 2nd functor. The composition of two functors is a functor whose map is the composition of the corresponding maps // mapOptionList = mapOption compose mapList assert(mapOptionList(square)(Some(List(1,2,3))) == mapOption(mapList(square))(Some(List(1,2,3)))) assert(mapOptionList(square)(Some(List(1,2,3))) == Some(List(1,4,9))) assert((mapOption(mapList(square))(Some(List(1,2,3))) == Some(List(1,4,9))) @philip_schwarz
  • 8. To get a fuller picture of the notion of a composite Functor, let’s look at Functor laws. See the following for a more comprehensive introduction to Functor laws. @philip_schwarz https://www.slideshare.net/pjschwarz/functor-laws
  • 9. A B C f g g ∘ f idB idB ∘ f F[A] F[B] F[C] f↑F g↑F g↑F ∘ f↑F idB↑F f↑F ∘ idB↑F C D h idC h ∘ idC F[C] F[D] h↑F idC↑F idC↑F ∘ h↑F C1 C2 C1 = C2 = Scala types and functions • objects: types • arrows: functions • composition operation: compose function, denoted here by ∘ • identity arrows: identity function T => T, denoted here by idT A functor F from C1 to C2 consisting of • a type constructor F that maps type A to F[A] • a map function from function f:A=>B to function f↑F :F[A] => F[B] Functor Laws F(g ∘ f) = F(g) ∘ F(f) i.e. map(g ∘ f) = map(g) ∘ map(f) F(idX) = idF(X) i.e. map(idX) = idX↑ F F[B]B f↑F is function f lifted into context F F[A] is type A lifted into context F idX↑ F is idX lifted into context F F F F F(g ∘ f) = F(g) ∘ F(f) map(g ∘ f) = map(g) ∘ map(f) F(A) = F[A] F(B) = F[B] F(C) = F[C] F(f:A=>B) = map(f) = f↑F:F[A]=>F[B] F(g:B=>C) = map(g) = g↑F:F[B]=>F[C] F(g∘f:A=>C) = map(g∘f) = g↑F∘f↑F:F[A]=>F[C] the mapping of the composition is the composition of the mappings A Functor from the category of ‘Scala types and functions’ to itself
  • 10. A B C f g g ∘ f idB idB ∘ f = f F[A] F[B] F[C] f↑F g↑F g↑F ∘ f↑F idB↑F idB↑F ∘ f↑F = f↑F C D h idC h ∘ idC F[C] F[D] h↑F idC↑F h↑F∘idC↑F = h↑F C1 C2 F[B]B F F F F(idX) = idF(X) map(idX) = idX↑F the mapping of an arrow’s identity is the identity of the arrow’s mapping F F F F(A) = F[A] F(B) = F[B] F(C) = F[N] F(D) = F[P] F(f) = f↑F F(h) = h↑F F(idB) = idB↑F F(idC) = idC↑F
  • 11. Now let’s revisit that with the notion of a composite Functor in mind. @philip_schwarz
  • 12. A B C f g g ∘ f F[A] F[B] F[C] f↑F g↑F g↑F ∘ f↑F G[F[A]] G[F[B]] G[F[C]] (f↑F)↑G (g↑F)↑G (g↑F)↑G ∘ (f↑F)↑G Functor F the mapping of the composition is the composition of the mappings The composition of Functors G and F is a composite functor G ∘ F from C1 to C2 consisting of • a type constructor • a mapG∘F function from function f:A=>B to function (f↑F)↑G: G[F[A]] => G[F[B]] where mapG∘F = mapG ∘ mapF G∘F(idX) = idG∘F(X) i.e. mapG∘F(idX) = (idX↑ F)↑ G the mapping of an arrow’s identity is the identity of the arrow’s mapping G∘F(g ∘ f) = (G∘F(g)) ∘ (G∘F(f)) i.e. mapG∘F(g ∘ f) = (mapG∘F (g)) ∘ (mapG∘F (f)) Here is what the Functor Laws look like for a composite Functor To keep the diagram simple, we are only illustrating the first law and we are not showing the Functor mappings between functions, e.g. f F f↑F G (f↑F)↑G Functions: f, g, g ∘ f, f↑F, g↑F, g↑F∘f↑F, etc Higher Order functions: mapF, mapG, mapG∘F mapF mapG
  • 13. // mapOptionList = mapOption compose mapList assert(((mapOptionList(inc compose twice))(Some(List(1,2,3)))) == (mapOption(mapList(inc)))((mapOption(mapList(twice)))(Some(List(1,2,3))))) assert((mapOption(mapList(inc)))((mapOption(mapList(twice)))(Some(List(1,2,3)))) == Some(List(3,5,7))) assert(((mapOptionList(inc compose twice))(Some(List(1,2,3)))) == Some(List(3,5,7))) // mapOptionList = mapOption compose mapList assert(mapOptionList(square)(Some(List(1,2,3))) == mapOption(mapList(square))(Some(List(1,2,3)))) assert(mapOptionList(square)(Some(List(1,2,3))) == Some(List(1,4,9))) assert((mapOption(mapList(square))(Some(List(1,2,3))) == Some(List(1,4,9))) Mapping the composition of g and f with the composition of two functors is the same as doing the following: 1. mapping f first with the 1st functor and then with the 2nd functor 2. mapping g first with the 1st functor and then with the 2nd functor 3. applying first the function computed in (1) and then the function computed in (2) Mapping f with the composition of two functors is the same as first mapping f with the 1st functor and then mapping the result with the 2nd functor. assert( (mapList(inc compose twice))(List(1,2,3)) == (mapList(inc))(mapList(twice)(List(1,2,3))) ) assert( (mapList(inc compose twice))(List(1,2,3)) == List(3,5,7) ) assert( (mapList(inc))(mapList(twice)(List(1,2,3))) == List(3,5,7) ) Mapping the composition of f and g is the same as first applying the mapping of f and then applying the mapping of g. 1st Functor Law 1st Functor Law and Functor Composition together Functor Composition The composition of two functors is a functor whose map is the composition of the corresponding maps def inc: Int => Int = _ + 1 def twice: Int => Int = _ * 2
  • 14. package scalaz private trait CompositionFunctor[F[_], G[_]] extends Functor[λ[α => F[G[α]]]] { implicit def F: Functor[F] implicit def G: Functor[G] override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] = F(fga)(G.lift(f)) } In Scalaz there is a CompositionFunctor trait Let’s adopt a similar approach to provide a functor composition trait trait CompositionFunctor[F[_],G[_]] extends Functor[λ[α => F[G[α]]]] { implicit def F: Functor[F] implicit def G: Functor[G] override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] = F.map(fga)(ga => G.map(ga)(f)) }
  • 15. Let’s see two examples of composing functors using our CompositionFunctor @philip_schwarz
  • 16. implicit val listFunctor = new Functor[List] { def map[A, B](fa: List[A])(f: A => B): List[B] = fa map f } implicit val optionFunctor = new Functor[Option] { def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa map f } trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B): F[B] } trait CompositionFunctor[F[_],G[_]] extends Functor[λ[α => F[G[α]]]] { implicit def F: Functor[F] implicit def G: Functor[G] override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] = F.map(fga)(ga => G.map(ga)(f)) } val listOptionFunctor = new CompositionFunctor[List,Option] { implicit def F: Functor[List] = listFunctor implicit def G: Functor[Option] = optionFunctor } val twice: Int => Int = _ * 2 val listOfOption = List(Some(3),None,Some(4)) val doubledListOfOption = List(Some(6),None,Some(8)) assert(listOptionFunctor.map(listOfOption)(twice) == doubledListOfOption) using https://github.com/non/kind-projector allows us to simplify type lambda ({type f[α] = F[G[α]]})#f to this: λ[α => F[G[α]]] Composing the List Functor with the Option Functor.
  • 17. implicit val listFunctor = new Functor[List] { def map[A, B](fa: List[A])(f: A => B): List[B] = fa map f } import scala.concurrent.ExecutionContext.Implicits.global val futureFunctor = new Functor[Future] { def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa map f (global) } val futureOptionFunctor = new CompositionFunctor[Future,Option] { implicit def F: Functor[Future] = futureFunctor implicit def G: Functor[Option] = optionFunctor } assert( Await.result( futureOptionFunctor.map(Future{ Some(3) })(twice), Duration.Inf) == Some(6) ) Composing the Option Functor with the Future Functor. trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B): F[B] } trait CompositionFunctor[F[_],G[_]] extends Functor[λ[α => F[G[α]]]] { implicit def F: Functor[F] implicit def G: Functor[G] override def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] = F.map(fga)(ga => G.map(ga)(f)) }
  • 18. Instead of having a trait, let’s have a compose method on Functor @philip_schwarz
  • 19. trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B): F[B] def compose[G[_]](G:Functor[G]):Functor[λ[α=>F[G[α]]]] = { val self = this new Functor[λ[α => F[G[α]]]] { override def map[A, B](fga:F[G[A]])(f:A=>B):F[G[B]] = self.map(fga)(ga => G.map(ga)(f)) } } } // Functor[List[Option]] = Functor[List] compose Functor[Option] val optionListFunctor = listFunctor compose optionFunctor assert(optionListFunctor.map(List(Some(1),Some(2),Some(3)))(double) == List(Some(2),Some(4),Some(6))) // Functor[Option[List]] = Functor[Option] compose Functor[List] val listOptionFunctor = optionFunctor compose listFunctor assert(listOptionFunctor.map(Some(List(1,2,3)))(double) == Some(List(2,4,6))) val double: Int => Int = _ * 2 implicit val listFunctor = new Functor[List] { def map[A, B](fa: List[A])(f: A => B): List[B] = fa map f } implicit val optionFunctor = new Functor[Option] { def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa map f } So that we van create a composite functor with listFunctor compose optionFunctor rather than with new CompositionFunctor[Future[Option]]{ … … }