SlideShare a Scribd company logo
1 of 13
Download to read offline
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

More Related Content

What's hot

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Philip Schwarz
 
The Expression Problem - Part 1
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfJaroslavRegec1
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackGaryCoady
 
Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...Philip Schwarz
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldPhilip Schwarz
 
Etabs concrete-design
Etabs concrete-designEtabs concrete-design
Etabs concrete-designmamilli
 
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
 
Using hilt in a modularized project
Using hilt in a modularized projectUsing hilt in a modularized project
Using hilt in a modularized projectFabio Collini
 
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
 
PyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in PracticePyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in PracticeSeomgi Han
 
How to build microservices with node.js
How to build microservices with node.jsHow to build microservices with node.js
How to build microservices with node.jsKaty Slemon
 
RxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixRxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixTracy Lee
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use casesFabio Biondi
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Philip Schwarz
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Philip Schwarz
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Philip Schwarz
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutinesRoman Elizarov
 

What's hot (20)

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
 
The Expression Problem - Part 1
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdf
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web Stack
 
Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
Etabs concrete-design
Etabs concrete-designEtabs concrete-design
Etabs concrete-design
 
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...
 
Using hilt in a modularized project
Using hilt in a modularized projectUsing hilt in a modularized project
Using hilt in a modularized project
 
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 ...
 
PyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in PracticePyCon KR 2018 Effective Tips for Django ORM in Practice
PyCon KR 2018 Effective Tips for Django ORM in Practice
 
How to build microservices with node.js
How to build microservices with node.jsHow to build microservices with node.js
How to build microservices with node.js
 
RxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixRxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMix
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use cases
 
Ngrx
NgrxNgrx
Ngrx
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
 
Sequence and Traverse - Part 2
Sequence and Traverse - Part 2Sequence and Traverse - Part 2
Sequence and Traverse - Part 2
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 

Similar to 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 categorytheoryMeetu Maltiar
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
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
 

Similar to 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 PPT
Thesis PPTThesis PPT
Thesis PPT
 
Thesis
ThesisThesis
Thesis
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 

More from Philip 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
 
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
 

More from Philip Schwarz (20)

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

Recently uploaded

Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 

Recently uploaded (20)

Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 

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