SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
A B
C
B
f
g
g	∘ f
idB
idB	∘ f	=	f
L M
N
M
u
v
v	∘ u
idM
idM	∘ u	=	u
C
D
h
idC
h	∘ idC	=	h
N
P
w
idN
w	∘ idN=	w
C1 and C2 are categories and ∘	denotes their composition operations. For every category object X there is an identity arrow idX:X→X such that for
every category arrow f:A→B we have idB ∘ f = f = f ∘ idA.
There is a functor F from C1 to C2 (a category homomorphism) which maps each C1 object to a C2 object and maps each C1 arrow to a C2 arrow in
such a way that the following two functor laws are satisfied (i.e. in such a way that composition and identity are preserved):
1)	F(g	∘	f) = F(g) ∘	F(f) - mapping the composition of two arrows is the same as composing the mapping of the 1st arrow and the mapping of 2nd arrow
2) F(idX) = idF(X) - the mapping of an object’s identity is the same as the identity of the object’s mapping
C1 C2
F(A)	=	L
F(B)	=	M
F(C)	=	N
F(D)	=	P
F(f)	=	u
F(g)	=	v
F(h)	=	wF(g	∘	f)	=	F(g)	∘	F(f)
F
F
F
the	mapping of	the	composition is	
the	composition of	the	mappings
Functor	Laws
A B
C
B
f
g
g	∘ f
idB
idB	∘ f	=	f
L M
N
M
u
v
v	∘ u
idM
idM	∘ u	=	u
C
D
h
idC
h	∘ idC	=	h
N
P
w
idN
w	∘ idN=	w
C1 C2
F(idX)	=	idF(X)
F
F
F
F
F
F
the	mapping	of	an	object’s	identity	is	
the	identity of	the	object’s	mapping
F(A)	=	L
F(B)	=	M
F(C)	=	N
F(D)	=	P
F(f)	=	u
F(h)	=	w
F(idB)	=	idM
F(idC)	=	idN
✽ ✽
✽
✽
“abc”
“de”
“abc”	+ “de”
“”“abc“	+	“”
✶ ✶
✶
✶
3
2
3	+ 2
0
3	+ 0
✽
✽
”f”
“”
“” + “f”
✶
✶
1
0
0	+ 1
C1 C2 F(✽)	=	✶
F(“abc”) = length(“abc”)	=	3
F(“de”)		 = length(“de”)	=	2
F(“abc”+”de”) = length(“abc”+”de”)	=	5
C1	=	Monoid(String,	+,	””)
• objects:	just	one,	denoted	by	✽
• arrows:	strings
• composition	operation:		string	concatenation
• identity	arrows:	the	empty	string
C2	=	Monoid	(Int,	+,	0)
• objects:	just	one,	denoted	by	✶
• arrows:	integer	numbers
• composition	operation:		integer	addition
• identity	arrows:	the	number	zero
A functor	F from	C1	to	C2	that	maps	✽ to	✶ and	maps	
a	string	to	its	length.	The	functor	laws
• F(g	∘	f)	=	F(g)	∘	F(f)	
• F(idX)	=	idF(X)
become
• length(s1	+ s2)	=	length(s1)	+	length(s2)	
• length(“”)	=	0	
F(s1	∘	s2)	=	F(s1)	∘	F(s2)
length(s1	+ s2)	=	length(s1)	+	length(s2)
“abcde” 5
F
F
F
the	mapping of	the	composition is	
the	composition of	the	mappings
Example:	a	Functor	from	one	Monoid	to	another
✽ ✽
✽
✽
“abc”
“de”
“abc”	+ “de”
“”“abc“+“”	=	“abc”
✶ ✶
✶
✶
3
2
3+2
0
3+0	=	3
✽
✽
”f”
“”
“” + “f”
✶
✶
1
0
0+1	=	1
C1 C2
F(idX)	=	idF(X)
length(“”)	=	0	
F
F
F
F
F
F
the	mapping	of	an	object’s	identity	is	
the	identity of	the	object’s	mapping
F(✽)	=	✶
F(“”) = length(“”)	=	0
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]	
So	F(g	∘	f)	=	F(g)	∘	F(f)		becomes		map(g	∘	f)	=	map(g)	∘	map(f)	
and	F(idX)	=	idF(X)	 becomes	 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
Example:	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
String Char
Int
f
g
g	∘ f
idChar
idChar	∘ 	f
Option[String] Option[Char]
f↑F
g↑F
g↑F ∘ f↑F
idB↑F
f↑F		∘		idChar↑F	
Int
Float
h
idInt
h	∘ idInt
h↑F
idInt ↑F
idInt↑F	∘	h↑F	
C1 C2 Option[Char]Char F
F
F
F(g	∘	f)	=	F(g)	∘	F(f)
map(g	∘	f)	=	map(g)	∘	map(f)
F(String) = Option[String]
F(Char) = Option[Char]
F(Int) = Option[Int]
F(f:String=>Char) = map(f) = f↑F: Option[String] => Option[Char]
F(g:Char=>Int) = map(g) = g↑F: Option[Char] => Option[Int]
F(g∘f:A=>C) = map(g∘f) = g∘f↑F: Option[String] => Option[Int]
the	mapping of	the	composition is	
the	composition of	the	mappings
Option[Int] Option[Int]
Option[Float]
val f: String => Char = x => x.head
val g: Char => Int = x => x.toInt
f("abc")
// Char = a
g('a')
// Int = 97
(g compose f)("abc")
// Int = 97
Some("abc") map (g compose f)
// Option[Int] = Some(97)
Some("abc") map f map g
// Option[Int] = Some(97)
None map (g compose f)
// Option[Int] = None
None map f map g
// Option [Int] = None
Making	the	Scala	example	more	concrete	with	an	actual	type	constructor:	Option
String Char
Int
f
g
g	∘ f
idChar
idChar	∘ f = f
f↑F
g↑F
g↑F ∘ f↑F
idChar↑F
idChar↑F	∘ f↑F =	f↑F
Float
h
idInt
h	∘ idInt
h↑F
idInt↑F
h↑F∘idInt↑F	=	h↑F	
C1 C2Char
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(String) = Option[String]
F(Char) = Option[Char]
F(Int) = Option[Int]
Int
Option[String] Option[Char]
Option[Int]
Option[Float]
Option[Char]
Option[Int]
val f: String => Char = x => x.head
val charId: Char => Char = x => x
val liftedCharId: Option[Char] => Option[Char] = x => x
F(f) = f↑F
F(h) = h↑F
F(idInt) = idInt↑F
F(idChar) = idChar↑F
Some("abc") map f map charId
// Option[Char] = Some(a)
liftedCharId(Some("abc") map f)
// Option[Char] = Some(a)
Some("abc") map f map charId
// Option[Char] = Some(a)
Some("abc") map (charId compose f)
// Option[Char] = Some(a)
liftedCharId(Some("abc") map f)
// Option[Char] = Some(a)
Some("abc") map (charId compose f)
// Option[Char] = Some(a)
trait Functor[F[_]] {
def map[A,B](f: A => B):F[A] => F[B]
}
The	scala	Functor	abstraction	typically	looks	like	this:
Examples	of	the	functor	laws	in	action	are	easier	to	follow	if	instead	of	using	the	customary	signature	of	map,	we	rearrange	
it	by	first	swapping	its	two	parameters	and	then	uncurrying	the	second	parameter:
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B):F[B]
}
trait Functor[F[_]] {
def map[A,B](f: A => B)(fa: F[A]):F[B]
}
customary	signature
after	swapping	parameters
after	uncurrying	the	second	parameter
swapping	of	fa and	f parameters
uncurrying	of	fa parameter
Scala	Functor	abstraction	– ‘map	method	implementation’	and	‘functor	laws	in	action’	for	Option
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B):F[B]
}
trait Functor[F[_]] {
def map[A,B](f: A => B):F[A] => F[B]
}
val optionF = new Functor[Option]{
def map[A,B](f: A => B):Option[A] => Option[B] = {
case Some(a) => Some(f(a))
case None => None
}
}
val increment:Int=>Int = x => x + 1
val twice:Int=>Int = x => 2 * x
val arrow1 = increment
val arrow2 = twice
val arrowMapping:Option[Int]=>Option[Int] = optionF.map(arrow)
val identityMapping:Option[Int]=>Option[Int] = optionF.map(arrowOutputIdentity)
// mapping the identity of an arrow’s output type is the same as mapping
// the arrow and taking the identity of the result’s output type: they have
// the same effect when composed with the mapping of the arrow
assert((identityMapping compose arrowMapping)(Some(3)) == (arrowMappingOutputIdentity compose arrowMapping)(Some(3)))
// mapping an arrow and the identity of its output type and composing them is the same
// as mapping the composition of the arrow with the identity of its output type
assert((identityMapping compose arrowMapping)(Some(3)) == optionF.map(arrowOutputIdentity compose arrow)(Some(3)))
// mapping an arrow and composing it with the identity of the result’s output type is the same
// as mapping the composition of the arrow with the identity of its output type
assert((arrowMappingOutputIdentity compose arrowMapping)(Some(3)) == optionF.map(arrowOutputIdentity compose arrow)(Some(3)))
val increment:Int=>Int = x => x + 1
val arrow = increment
val arrowOutputIdentity:Int=>Int = x => x
val arrowMappingOutputIdentity:Option[Int]=>Option[Int]=x=>x
val mappingOfArrowComposition = optionF.map(arrow1 compose arrow2)
val compositionOfArrowMappings = optionF.map(arrow1) compose optionF.map(arrow2)
// mapping the composition of two arrows is the same as mapping the arrows and composing them
assert(mappingOfArrowComposition(Some(3)) == compositionOfArrowMappings(Some(3)))
assert(mappingOfArrowComposition(None) == compositionOfArrowMappings(None))
the	mapping of	the	composition is	
the	composition	of	the	mappings
the	mapping	of	an	object’s	identity	is	
the	identity of	the	object’s	mapping
F(g	∘	f)	=	F(g)	∘	F(f)
map(g	∘	f)	=	map(g)	∘	map(f)
F(idX)	=	idF(X)
map(idX)	=	idX↑F	
Example	of	Functor	abstraction	and	Functor	laws	in	action,	using	the	more	
convenient	map signature.	
The	signature	is	more	convenient	in	that	it	allows	us	to	compose	the	
mappings	by	literally	performing	their	(function)	composition.
val appliedArrowMapping = optionF.map(Some(3))(arrow)
// mapping the identity of an arrow’s output type is the same as mapping
// the arrow and taking the identity of the result’s output type: they have
// the same effect when composed with the mapping of the arrow
assert(optionF.map(appliedArrowMapping)(arrowOutputIdentity) == arrowMappingOutputIdentity(appliedArrowMapping))
// mapping an arrow and the identity of its output type and composing them is the same
// as mapping the composition of the arrow with the identity of its output type
assert(optionF.map(appliedArrowMapping)(arrowOutputIdentity) == optionF.map(Some(3))(arrowOutputIdentity compose arrow))
// mapping an arrow and composing it with the identity of the result’s output type is the same
// as mapping the composition of the arrow with the identity of its output type
assert(arrowMappingOutputIdentity(appliedArrowMapping) == optionF.map(Some(3))(arrowOutputIdentity compose arrow))
the	mapping	of	an	arrow’s	identity	is	
the	identity of	the	arrow’s	mapping
F(idX)	=	idF(X)
map(idX)	=	idX↑F	
var mappingOfArrowComposition = optionF.map(Some(3))(arrow1 compose arrow2)
var compositionOfArrowMappings = optionF.map(optionF.map(Some(3))(arrow2))(arrow1)
// mapping the composition of two arrows is the same as mapping the arrows and composing them
assert(mappingOfArrowComposition == compositionOfArrowMappings)
mappingOfArrowComposition = optionF.map(None)(arrow1 compose arrow2)
compositionOfArrowMappings = optionF.map(optionF.map(None)(arrow2))(arrow1)
assert(mappingOfArrowComposition == compositionOfArrowMappings)
the	mapping of	the	composition is	
the	composition	of	the	mappings
F(g	∘	f)	=	F(g)	∘	F(f)
map(g	∘	f)	=	map(g)	∘	map(f)
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B):F[B]
}
val optionF = new Functor[Option] {
def map[A,B](fa:Option[A])(f: A => B): Option[B] =
fa match {
case Some(a) => Some(f(a))
case None => None
}
}
val increment:Int=>Int = x => x + 1
val arrow = increment
val arrowOutputIdentity:Int=>Int = x => x
val arrowMappingOutputIdentity:Option[Int]=>Option[Int] = x=>x
Example	of	Functor	abstraction	and	Functor	laws	in	action,	using	the	
customary	map signature.	
The signature is less convenient in that rather than allowing us to compose
the mappings by literally performing their (function) composition, it requires
us to do so by chaining map invocations and calling functions.
val increment:Int=>Int = x => x + 1
val twice:Int=>Int = x => 2 * x
val arrow1 = increment
val arrow2 = twice
by Paul Chiusano and
Runar Bjarnason
https://twitter.com/runarorama
https://twitter.com/pchiusano
11.1.1 Functor laws
Whenever we create an abstraction like Functor, we should consider not only what abstract methods it should have,
but which laws we expect to hold for the implementations. The laws you stipulate for an abstraction are entirely up to
you, and of course Scala won’t enforce any of these laws.
…
For Functor, we’ll stipulate the familiar law we first introduced in chapter 7 for our Par data type:
map(x)(a => a) == x
In other words, mapping over a structure x with the identity function should itself be an identity. This law is quite
natural, and we noticed later in part 2 that this law was satisfied by the map functions of other types besides Par. This
law (and its corollaries given by parametricity) capture the requirement that map(x) “preserves the structure” of
x. Implementations satisfying this law are restricted from doing strange things like throwing exceptions, removing
the first element of a List, converting a Some to None, and so on. Only the elements of the structure are
modified by map; the shape or structure itself is left intact. Note that this law holds for List, Option, Par, Gen,
and most other data types that define map!
The	Functor	Laws	mean	that	a	Functor’s	map is	structure	preserving
“Only the elements of the structure are modified by map; the shape or structure itself is left intact.”
by Paul Chiusano and
Runar Bjarnason
https://twitter.com/runarorama
https://twitter.com/pchiusano
map(y)(id) == y
…To get some insight into what this new law is saying, let’s think about what map can’t do. It can’t, say, throw an
exception and crash the computation before applying the function to the result (can you see why this violates the law?). All
it can do is apply the function f to the result of y, which of course leaves y unaffected when that function is id.11
Even more interestingly, given map(y)(id) == y
…
it must be true that map(unit(x))(f) == unit(f(x)) . Since we get this second law or theorem for free, simply
because of the parametricity of map , it’s sometimes called a free theorem.12
EXERCISE	7.7
Hard: Given map(y)(id) == y, it’s a free theorem that map(map(y)(g))(f) == map(y)(f compose g). (This is
sometimes called map fusion, and it can be used as an optimization—rather than spawning a separate parallel
computation to compute the second mapping, we can fold it into the first mapping.)13 Can you prove it? You may want
to read the paper “Theorems for Free!” (http://mng.bz/Z9f1) to better understand the “trick” of free theorems.
…
11 We say that map is required to be structure-preserving in that it doesn’t alter the structure of the parallel
computation, only the value “inside” the computation.
12 The idea of free theorems was introduced by Philip Wadler in the classic paper “Theorems for Free!”
(http://mng.bz/Z9f1).
A	Functor’s	composition	law	is	derivable	from	its	identity	law	as	a	free	theorem

Contenu connexe

Tendances

Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Eliran Eliassy
 
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Ruslan Shevchenko
 
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
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsYura Nosenko
 
Jetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UIJetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UIGilang Ramadhan
 
CI CD Jenkins for Swift Deployment
CI CD Jenkins for Swift DeploymentCI CD Jenkins for Swift Deployment
CI CD Jenkins for Swift DeploymentBintang Thunder
 
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021Natan Silnitsky
 
Belajar Latex dalam 30 menit dengan menggunakan overleaf
Belajar Latex dalam 30 menit dengan menggunakan overleafBelajar Latex dalam 30 menit dengan menggunakan overleaf
Belajar Latex dalam 30 menit dengan menggunakan overleafIC Magnet School
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersPhilip Schwarz
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptWill Livengood
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and HaskellHermann Hueck
 
The Power Of Composition (DotNext 2019)
The Power Of Composition (DotNext 2019)The Power Of Composition (DotNext 2019)
The Power Of Composition (DotNext 2019)Scott Wlaschin
 
Android kotlin coroutines
Android kotlin coroutinesAndroid kotlin coroutines
Android kotlin coroutinesBipin Vayalu
 

Tendances (20)

Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics Angular server side rendering - Strategies & Technics
Angular server side rendering - Strategies & Technics
 
Refactoring
RefactoringRefactoring
Refactoring
 
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
 
Angular
AngularAngular
Angular
 
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...
 
BitBucket vs GoogleCode
BitBucket vs GoogleCodeBitBucket vs GoogleCode
BitBucket vs GoogleCode
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applications
 
Jetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UIJetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UI
 
CI CD Jenkins for Swift Deployment
CI CD Jenkins for Swift DeploymentCI CD Jenkins for Swift Deployment
CI CD Jenkins for Swift Deployment
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
 
Belajar Latex dalam 30 menit dengan menggunakan overleaf
Belajar Latex dalam 30 menit dengan menggunakan overleafBelajar Latex dalam 30 menit dengan menggunakan overleaf
Belajar Latex dalam 30 menit dengan menggunakan overleaf
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parameters
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
Manual abap
Manual abapManual abap
Manual abap
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
 
Training On Angular Js
Training On Angular JsTraining On Angular Js
Training On Angular Js
 
The Power Of Composition (DotNext 2019)
The Power Of Composition (DotNext 2019)The Power Of Composition (DotNext 2019)
The Power Of Composition (DotNext 2019)
 
Android kotlin coroutines
Android kotlin coroutinesAndroid kotlin coroutines
Android kotlin coroutines
 

Similaire à Functor Laws

The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator PatternEric Torreborre
 
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
 
Category theory for beginners
Category theory for beginnersCategory theory for beginners
Category theory for beginnerskenbot
 
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 with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adtsHang Zhao
 
Monad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional ProgrammingMonad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional ProgrammingNamuk Park
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystifiedAlessandro Lacava
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGDavid Galichet
 
(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
 
Taking your side effects aside
Taking your side effects asideTaking your side effects aside
Taking your side effects aside💡 Tomasz Kogut
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 

Similaire à Functor Laws (20)

The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
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)
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
 
Category theory for beginners
Category theory for beginnersCategory theory for beginners
Category theory for beginners
 
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
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 
Functor Composition
Functor CompositionFunctor Composition
Functor Composition
 
Monad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional ProgrammingMonad and Algebraic Design in Functional Programming
Monad and Algebraic Design in Functional Programming
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystified
 
Introducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUGIntroducing Monads and State Monad at PSUG
Introducing Monads and State Monad at PSUG
 
(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
 
Taking your side effects aside
Taking your side effects asideTaking your side effects aside
Taking your side effects aside
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 
Thesis
ThesisThesis
Thesis
 
Thesis PPT
Thesis PPTThesis PPT
Thesis PPT
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 

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

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

Dernier

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
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
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
 
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
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%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
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+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
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%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
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 

Dernier (20)

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
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
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
 
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...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%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
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+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...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%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
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 

Functor Laws

  • 1. A B C B f g g ∘ f idB idB ∘ f = f L M N M u v v ∘ u idM idM ∘ u = u C D h idC h ∘ idC = h N P w idN w ∘ idN= w C1 and C2 are categories and ∘ denotes their composition operations. For every category object X there is an identity arrow idX:X→X such that for every category arrow f:A→B we have idB ∘ f = f = f ∘ idA. There is a functor F from C1 to C2 (a category homomorphism) which maps each C1 object to a C2 object and maps each C1 arrow to a C2 arrow in such a way that the following two functor laws are satisfied (i.e. in such a way that composition and identity are preserved): 1) F(g ∘ f) = F(g) ∘ F(f) - mapping the composition of two arrows is the same as composing the mapping of the 1st arrow and the mapping of 2nd arrow 2) F(idX) = idF(X) - the mapping of an object’s identity is the same as the identity of the object’s mapping C1 C2 F(A) = L F(B) = M F(C) = N F(D) = P F(f) = u F(g) = v F(h) = wF(g ∘ f) = F(g) ∘ F(f) F F F the mapping of the composition is the composition of the mappings Functor Laws
  • 2. A B C B f g g ∘ f idB idB ∘ f = f L M N M u v v ∘ u idM idM ∘ u = u C D h idC h ∘ idC = h N P w idN w ∘ idN= w C1 C2 F(idX) = idF(X) F F F F F F the mapping of an object’s identity is the identity of the object’s mapping F(A) = L F(B) = M F(C) = N F(D) = P F(f) = u F(h) = w F(idB) = idM F(idC) = idN
  • 3. ✽ ✽ ✽ ✽ “abc” “de” “abc” + “de” “”“abc“ + “” ✶ ✶ ✶ ✶ 3 2 3 + 2 0 3 + 0 ✽ ✽ ”f” “” “” + “f” ✶ ✶ 1 0 0 + 1 C1 C2 F(✽) = ✶ F(“abc”) = length(“abc”) = 3 F(“de”) = length(“de”) = 2 F(“abc”+”de”) = length(“abc”+”de”) = 5 C1 = Monoid(String, +, ””) • objects: just one, denoted by ✽ • arrows: strings • composition operation: string concatenation • identity arrows: the empty string C2 = Monoid (Int, +, 0) • objects: just one, denoted by ✶ • arrows: integer numbers • composition operation: integer addition • identity arrows: the number zero A functor F from C1 to C2 that maps ✽ to ✶ and maps a string to its length. The functor laws • F(g ∘ f) = F(g) ∘ F(f) • F(idX) = idF(X) become • length(s1 + s2) = length(s1) + length(s2) • length(“”) = 0 F(s1 ∘ s2) = F(s1) ∘ F(s2) length(s1 + s2) = length(s1) + length(s2) “abcde” 5 F F F the mapping of the composition is the composition of the mappings Example: a Functor from one Monoid to another
  • 4. ✽ ✽ ✽ ✽ “abc” “de” “abc” + “de” “”“abc“+“” = “abc” ✶ ✶ ✶ ✶ 3 2 3+2 0 3+0 = 3 ✽ ✽ ”f” “” “” + “f” ✶ ✶ 1 0 0+1 = 1 C1 C2 F(idX) = idF(X) length(“”) = 0 F F F F F F the mapping of an object’s identity is the identity of the object’s mapping F(✽) = ✶ F(“”) = length(“”) = 0
  • 5. 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] So F(g ∘ f) = F(g) ∘ F(f) becomes map(g ∘ f) = map(g) ∘ map(f) and F(idX) = idF(X) becomes 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 Example: a Functor from the category of ‘Scala types and functions’ to itself
  • 6. 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
  • 7. String Char Int f g g ∘ f idChar idChar ∘ f Option[String] Option[Char] f↑F g↑F g↑F ∘ f↑F idB↑F f↑F ∘ idChar↑F Int Float h idInt h ∘ idInt h↑F idInt ↑F idInt↑F ∘ h↑F C1 C2 Option[Char]Char F F F F(g ∘ f) = F(g) ∘ F(f) map(g ∘ f) = map(g) ∘ map(f) F(String) = Option[String] F(Char) = Option[Char] F(Int) = Option[Int] F(f:String=>Char) = map(f) = f↑F: Option[String] => Option[Char] F(g:Char=>Int) = map(g) = g↑F: Option[Char] => Option[Int] F(g∘f:A=>C) = map(g∘f) = g∘f↑F: Option[String] => Option[Int] the mapping of the composition is the composition of the mappings Option[Int] Option[Int] Option[Float] val f: String => Char = x => x.head val g: Char => Int = x => x.toInt f("abc") // Char = a g('a') // Int = 97 (g compose f)("abc") // Int = 97 Some("abc") map (g compose f) // Option[Int] = Some(97) Some("abc") map f map g // Option[Int] = Some(97) None map (g compose f) // Option[Int] = None None map f map g // Option [Int] = None Making the Scala example more concrete with an actual type constructor: Option
  • 8. String Char Int f g g ∘ f idChar idChar ∘ f = f f↑F g↑F g↑F ∘ f↑F idChar↑F idChar↑F ∘ f↑F = f↑F Float h idInt h ∘ idInt h↑F idInt↑F h↑F∘idInt↑F = h↑F C1 C2Char 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(String) = Option[String] F(Char) = Option[Char] F(Int) = Option[Int] Int Option[String] Option[Char] Option[Int] Option[Float] Option[Char] Option[Int] val f: String => Char = x => x.head val charId: Char => Char = x => x val liftedCharId: Option[Char] => Option[Char] = x => x F(f) = f↑F F(h) = h↑F F(idInt) = idInt↑F F(idChar) = idChar↑F Some("abc") map f map charId // Option[Char] = Some(a) liftedCharId(Some("abc") map f) // Option[Char] = Some(a) Some("abc") map f map charId // Option[Char] = Some(a) Some("abc") map (charId compose f) // Option[Char] = Some(a) liftedCharId(Some("abc") map f) // Option[Char] = Some(a) Some("abc") map (charId compose f) // Option[Char] = Some(a)
  • 9. trait Functor[F[_]] { def map[A,B](f: A => B):F[A] => F[B] } The scala Functor abstraction typically looks like this: Examples of the functor laws in action are easier to follow if instead of using the customary signature of map, we rearrange it by first swapping its two parameters and then uncurrying the second parameter: trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B):F[B] } trait Functor[F[_]] { def map[A,B](f: A => B)(fa: F[A]):F[B] } customary signature after swapping parameters after uncurrying the second parameter swapping of fa and f parameters uncurrying of fa parameter Scala Functor abstraction – ‘map method implementation’ and ‘functor laws in action’ for Option trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B):F[B] }
  • 10. trait Functor[F[_]] { def map[A,B](f: A => B):F[A] => F[B] } val optionF = new Functor[Option]{ def map[A,B](f: A => B):Option[A] => Option[B] = { case Some(a) => Some(f(a)) case None => None } } val increment:Int=>Int = x => x + 1 val twice:Int=>Int = x => 2 * x val arrow1 = increment val arrow2 = twice val arrowMapping:Option[Int]=>Option[Int] = optionF.map(arrow) val identityMapping:Option[Int]=>Option[Int] = optionF.map(arrowOutputIdentity) // mapping the identity of an arrow’s output type is the same as mapping // the arrow and taking the identity of the result’s output type: they have // the same effect when composed with the mapping of the arrow assert((identityMapping compose arrowMapping)(Some(3)) == (arrowMappingOutputIdentity compose arrowMapping)(Some(3))) // mapping an arrow and the identity of its output type and composing them is the same // as mapping the composition of the arrow with the identity of its output type assert((identityMapping compose arrowMapping)(Some(3)) == optionF.map(arrowOutputIdentity compose arrow)(Some(3))) // mapping an arrow and composing it with the identity of the result’s output type is the same // as mapping the composition of the arrow with the identity of its output type assert((arrowMappingOutputIdentity compose arrowMapping)(Some(3)) == optionF.map(arrowOutputIdentity compose arrow)(Some(3))) val increment:Int=>Int = x => x + 1 val arrow = increment val arrowOutputIdentity:Int=>Int = x => x val arrowMappingOutputIdentity:Option[Int]=>Option[Int]=x=>x val mappingOfArrowComposition = optionF.map(arrow1 compose arrow2) val compositionOfArrowMappings = optionF.map(arrow1) compose optionF.map(arrow2) // mapping the composition of two arrows is the same as mapping the arrows and composing them assert(mappingOfArrowComposition(Some(3)) == compositionOfArrowMappings(Some(3))) assert(mappingOfArrowComposition(None) == compositionOfArrowMappings(None)) the mapping of the composition is the composition of the mappings the mapping of an object’s identity is the identity of the object’s mapping F(g ∘ f) = F(g) ∘ F(f) map(g ∘ f) = map(g) ∘ map(f) F(idX) = idF(X) map(idX) = idX↑F Example of Functor abstraction and Functor laws in action, using the more convenient map signature. The signature is more convenient in that it allows us to compose the mappings by literally performing their (function) composition.
  • 11. val appliedArrowMapping = optionF.map(Some(3))(arrow) // mapping the identity of an arrow’s output type is the same as mapping // the arrow and taking the identity of the result’s output type: they have // the same effect when composed with the mapping of the arrow assert(optionF.map(appliedArrowMapping)(arrowOutputIdentity) == arrowMappingOutputIdentity(appliedArrowMapping)) // mapping an arrow and the identity of its output type and composing them is the same // as mapping the composition of the arrow with the identity of its output type assert(optionF.map(appliedArrowMapping)(arrowOutputIdentity) == optionF.map(Some(3))(arrowOutputIdentity compose arrow)) // mapping an arrow and composing it with the identity of the result’s output type is the same // as mapping the composition of the arrow with the identity of its output type assert(arrowMappingOutputIdentity(appliedArrowMapping) == optionF.map(Some(3))(arrowOutputIdentity compose arrow)) the mapping of an arrow’s identity is the identity of the arrow’s mapping F(idX) = idF(X) map(idX) = idX↑F var mappingOfArrowComposition = optionF.map(Some(3))(arrow1 compose arrow2) var compositionOfArrowMappings = optionF.map(optionF.map(Some(3))(arrow2))(arrow1) // mapping the composition of two arrows is the same as mapping the arrows and composing them assert(mappingOfArrowComposition == compositionOfArrowMappings) mappingOfArrowComposition = optionF.map(None)(arrow1 compose arrow2) compositionOfArrowMappings = optionF.map(optionF.map(None)(arrow2))(arrow1) assert(mappingOfArrowComposition == compositionOfArrowMappings) the mapping of the composition is the composition of the mappings F(g ∘ f) = F(g) ∘ F(f) map(g ∘ f) = map(g) ∘ map(f) trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B):F[B] } val optionF = new Functor[Option] { def map[A,B](fa:Option[A])(f: A => B): Option[B] = fa match { case Some(a) => Some(f(a)) case None => None } } val increment:Int=>Int = x => x + 1 val arrow = increment val arrowOutputIdentity:Int=>Int = x => x val arrowMappingOutputIdentity:Option[Int]=>Option[Int] = x=>x Example of Functor abstraction and Functor laws in action, using the customary map signature. The signature is less convenient in that rather than allowing us to compose the mappings by literally performing their (function) composition, it requires us to do so by chaining map invocations and calling functions. val increment:Int=>Int = x => x + 1 val twice:Int=>Int = x => 2 * x val arrow1 = increment val arrow2 = twice
  • 12. by Paul Chiusano and Runar Bjarnason https://twitter.com/runarorama https://twitter.com/pchiusano 11.1.1 Functor laws Whenever we create an abstraction like Functor, we should consider not only what abstract methods it should have, but which laws we expect to hold for the implementations. The laws you stipulate for an abstraction are entirely up to you, and of course Scala won’t enforce any of these laws. … For Functor, we’ll stipulate the familiar law we first introduced in chapter 7 for our Par data type: map(x)(a => a) == x In other words, mapping over a structure x with the identity function should itself be an identity. This law is quite natural, and we noticed later in part 2 that this law was satisfied by the map functions of other types besides Par. This law (and its corollaries given by parametricity) capture the requirement that map(x) “preserves the structure” of x. Implementations satisfying this law are restricted from doing strange things like throwing exceptions, removing the first element of a List, converting a Some to None, and so on. Only the elements of the structure are modified by map; the shape or structure itself is left intact. Note that this law holds for List, Option, Par, Gen, and most other data types that define map! The Functor Laws mean that a Functor’s map is structure preserving “Only the elements of the structure are modified by map; the shape or structure itself is left intact.”
  • 13. by Paul Chiusano and Runar Bjarnason https://twitter.com/runarorama https://twitter.com/pchiusano map(y)(id) == y …To get some insight into what this new law is saying, let’s think about what map can’t do. It can’t, say, throw an exception and crash the computation before applying the function to the result (can you see why this violates the law?). All it can do is apply the function f to the result of y, which of course leaves y unaffected when that function is id.11 Even more interestingly, given map(y)(id) == y … it must be true that map(unit(x))(f) == unit(f(x)) . Since we get this second law or theorem for free, simply because of the parametricity of map , it’s sometimes called a free theorem.12 EXERCISE 7.7 Hard: Given map(y)(id) == y, it’s a free theorem that map(map(y)(g))(f) == map(y)(f compose g). (This is sometimes called map fusion, and it can be used as an optimization—rather than spawning a separate parallel computation to compute the second mapping, we can fold it into the first mapping.)13 Can you prove it? You may want to read the paper “Theorems for Free!” (http://mng.bz/Z9f1) to better understand the “trick” of free theorems. … 11 We say that map is required to be structure-preserving in that it doesn’t alter the structure of the parallel computation, only the value “inside” the computation. 12 The idea of free theorems was introduced by Philip Wadler in the classic paper “Theorems for Free!” (http://mng.bz/Z9f1). A Functor’s composition law is derivable from its identity law as a free theorem