SlideShare a Scribd company logo
1 of 5
Download to read offline
So this is like the first level of category theory. The next level of category theory is functors. And it is like repeating sort of the same story at a higher level. And category theory is just like this: you make up a
story and then you repeat it at some other level. So here we are saying we know what a category is, so suppose we have more than one category, we have several categories. Can we look at a category? Again,
we started with sets and we said let’s forget about what is inside a set, let’s just look at connections between sets, now let’s forget what a category is, this is a crazy idea, let’s forget what is inside a category,
what are the arrows between categories? We want to define categories using arrows between categories. What’s an arrow between categories? …a category is objects and arrows. So an arrow between
categories would map objects to objects and would also map arrows to arrows. That’s a good function on categories. And it turns out that is the beginning of the definition of a functor. A functor is a
mapping between categories. Therefore it is a mapping from objects to objects and arrows to arrows. But it cannot just map arrows randomly. If you have an arrow that goes from A to B in one category then
A will be mapped into F[A] and B will be mapped to F[B].
It makes sense to map the arrow that goes from A to B to the arrow that goes from F[A] to F[B]. So a functor preserves connections. If there is a connection from A to B in one category then in the target
category there will be a connection between F[A] to F[B]. So it just preserves all the connections. Which makes sense. If we want to have a mapping between categories we don’t want to have random
mappings, we want to have mappings that are categorical, that preserve the structure. Now let’s go back to programming. What is a functor in programming? First of all, in programming we only have one
category…the category of types and functions but that is good enough. We can have a functor that goes from a category to the same category…so in programming we concentrate on these functors that
don’t leave the category, they stay in the same category, so the mapping of an object, what does it mean? Objects are types so we have to have a mapping from type to type, which is called a type
constructor: you give me a type and I’ll give you a new type. For instance, List is a functor. You give me a type, let’s say Int, and I’ll give you a List[Int]. So there is a mapping that is a type constructor.
What about arrows? If I have a function that goes from object A to B, or from type A to type B, so here is this f, I want to map it to an
arrow that will go from F[A] to F[B] and this mapping is called fmap. If you have seen a functor in Scala libraries, it has this function called
fmap, a higher order function.
So if you give me a function that goes from A to B I give you a function that goes from List[A] to List[B].
How is it implemented? Well, it applies the function to every element of the list. So that’s an example of a
functor. List is really a functor. It not only maps types to types, but also maps functions to functions. And
here is the definition written in Scala.
Bartosz	Milewski
https://twitter.com/BartoszMilewski
Bartosz	Milewski	explaining	Functors in	category	theory	as	part	of	his	Scala	eXchange	2017	Keynote:	The	Maths	Behind	Types
a	functor	is	a	mapping	between	categories…
a	mapping	from	type	to	type,	which	is	called	a	type	constructor…
a	mapping	from	function	to	function	called fmap…
e.g. List	is	a	functor.	You	give	me	a	type,	e.g.	Int,	and	I’ll	give	you	a	type	List[Int].	
You	give	me	a	function	that	goes	from A	to B	and	I	give	you	a	function	that	goes	
from	List[A]	to	List[B]
https://skillsmatter.com/skillscasts/10179-the-maths-behind-types#video
“In functional programming an effect adds some additional capabilities to a computation. And since we are dealing with a statically typed language, these capabilities come in the form
of more power from the type system. An effect is modeled usually in the form of a type constructor that constructs types with these additional capabilities. Say we have any type A and
we would like to add the capability of aggregation, so that we can treat a collection of A as a separate type. We do this by constructing a type List[A] (for which the corresponding type
constructor is List), which adds the effect of aggregation on A. Similarly we can have Option[A] that adds the capability of optionality for the type A.”
You can say that a functor abstracts the capability of mapping over a data structure with an ordinary function. Now you may ask what advantage does that bring in the context of domain
modeling? The answer is simple – it generalizes your model and makes some parts of your model behavior reusable across components.
…
A functor gives you the capability to lift a pure function of one argument into a specific context. Have a look at the definition of map in the trait Functor. The pure function f: A => B is
lifted into the context of F, transforming F[A] to F[B]. The context here is the type constructor F, also known as the effect of the computation.
Have you ever looked at the signatures of map in some of the classes of the Scala standard library like List, Option or Try?...the functions have the same
signature if you ignore the context of application. In terms of functional programming we call this context an effect.
def map[B](f: (A) => B): List[B] // map for List[A]
def map[B](f: (A) => B): Option[B] // map for Option[A]
def map[B](f: (A) => B): Try[B] // map for Try[A]
List[A] provides an effect of repetition of elements of type A, Option[A] models the effect of uncertainty where the element of type A can either exist or not.
Try[A] provides the effect of reification of exceptions.
So if you can abstract the effects out of the signatures, then all you have is a function map that lifts the function f into an effect F[_]. Note each of the
effects that we are talking about here is modeled using a type constructor – hence F[_] and not a simple F. We can extract this computation structure into a
separate trait of its own and call it the Functor.
trait Functor[F[_]] {
def map[A, B](a: F[A])(f: A => B): F[B]
}
Since List, Option etc. all share this computational behavior we can make them functors by implementing an interpreter of the functor algebra for each
of them.
def ListFunctor: Functor[List] = new Functor[List] {
def map[A, B](a: List[A])(f: A => B): List[B] = a map f
}
def OptionFunctor: Functor[Option] = new Functor[Option] {
def map[A, B](a: Option[A])(f: A => B): Option[B] = a map f
}
Debasish	Ghosh
https://twitter.com/debasishg
Debasish	Ghosh	explaining	Functors in	his	book	Functional	and	Reactive	Domain	Modeling - https://www.manning.com/books/functional-and-reactive-domain-modeling
4.3.2 Option composition, lifting, and wrapping exception-oriented APIs
It may be easy to jump to the conclusion that once we start using Option, it infects our entire code base. One can
imagine how any callers of methods that take or return Option will have to be modified to handle either Some or None.
But this doesn’t happen, and the reason is that we can lift ordinary functions to become functions that operate on
Option.
For example, the map function lets us operate on values of type Option[A] using a function of type A => B, returning
Option[B]. Another way of looking at this is that map turns a function f of type A => B into a function of type
Option[A] => Option[B]. Let’s make this explicit:
def lift[A,B](f: A => B): Option[A] => Option[B] = _ map f
This tells us that any function that we already have lying around can be transformed (via lift) to operate within the
context of a single Option value. Let’s look at an example:
val absO: Option[Double] => Option[Double] = lift(math.abs)
The math object contains various standalone mathematical functions including abs, sqrt, exp, and so on. We didn’t
need to rewrite the math.abs function to work with optional values; we just lifted it into the Option context after
the fact. We can do this for any function.
EXERCISE 3.18
Write a function map that generalizes modifying each element in a list while maintaining the structure of the list.
Here is its signature:
def map[A,B](as: List[A])(f: A => B): List[B]
Some	‘Functional	Programming	in	Scala’	excerpts	explaining	Functors https://www.manning.com/books/functional-programming-in-scala
by Paul Chiusano and
Runar Bjarnason
https://twitter.com/runarorama
https://twitter.com/pchiusano
11.1 Functors: generalizing the map function
In parts 1 and 2, we implemented several different combinator libraries. In each case, we proceeded by writing a small set of primitives and then a
number of combinators defined purely in terms of those primitives. We noted some similarities between derived combinators across the libraries we
wrote. For instance, we implemented a map function for each data type, to lift a function taking one argument “into the context of” some data
type. For Gen, Parser, and Option, the type signatures were as follows:
def map[A,B](ga: Gen[A])(f: A => B): Gen[B]
def map[A,B](pa: Parser[A])(f: A => B): Parser[B]
def map[A,B](oa: Option[A])(f: A => B): Option[B]
These type signatures differ only in the concrete data type (Gen, Parser, or Option ). We can capture as a Scala trait the idea of “a data type
that implements map”:
trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B): F[B] }
Here we’ve parameterized map on the type constructor, F[_], much like we did with Foldable in the previous chapter. Instead of picking a
particular F[_], like Gen or Parser, the Functor trait is parametric in the choice of F. Here’s an instance for List :
val listFunctor = new Functor[List] { def map[A,B](as: List[A])(f: A => B): List[B] = as map f }
We say that a type constructor like List (or Option, or F ) is a functor, and the Functor[F] instance constitutes proof that F is in fact a
functor. What can we do with this abstraction? As we did in several places throughout this book, we can discover useful functions just by
playing with the operations of the interface, in a purely algebraic way…
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.
…
For Functor , we’ll stipulate the familiar law we first introduced in chapter 7…
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 !
https://www.manning.com/books/functional-programming-in-scala
by Paul Chiusano and
Runar Bjarnason
https://twitter.com/runarorama
https://twitter.com/pchiusano
What’s a functor?
A functor is a way to apply a function over or around some structure that we don’t want to alter. That is,
we want to apply the function to the value that is “inside” some structure and leave the structure alone.
That’s why it is most common to introduce functor by way of fmapping over lists, as we did back in the Lists
chapter. The function gets applied to each value inside the list, and the list structure remains. A good way to
relate “not altering the structure” to lists is that the length of the list after mapping a function over it will
always be the same. No elements are removed or added, only transformed.
Monads sort of steal the Haskell spotlight, but you can do more with Functor and Applicative than many people
realize. Also, understanding Functor and Applicative is important to a deep understanding of Monad.
The definition of the Functor typeclass in Haskell looks like this:
class Functor f where
fmap :: (a -> b) -> f a -> f b
Functor Laws
Instances of the Functor typeclass should abide by two basic laws. Understanding these laws is critical for
understanding Functor and writing typeclass instances that are composable and easy to reason about.
Identity
The first law is the law of identity: fmap id == id
If we fmap the identity function, it should have the same result as passing our value to identity. We
shouldn’t be changing any of the outer structure 𝑓 that we’re mapping over by mapping id.
Composition
The second law for Functor is the law of composition: fmap (f . g) == fmap f . fmap g
This concerns the composability of fmap. If we compose two functions, 𝑓 and 𝑔, and fmap that over some
structure, we should get the same result as if we fmapped them and then composed them… If an
implementation of fmap doesn’t do that, it’s a broken functor.
http://haskellbook.com/
By Christopher Allen,
Julie Moronuki
https://twitter.com/bitemyapp
https://twitter.com/argumatronic
Some	‘Haskell Programming from First Principles’	excerpts	explaining	Functors

More Related Content

What's hot

Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Philip Schwarz
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Philip Schwarz
 
Kleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelationKleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelationPhilip Schwarz
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1Hang Zhao
 
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Philip Schwarz
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2Hang Zhao
 
Scala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data TypeScala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data TypePhilip Schwarz
 
Function Composition - forward composition versus backward composition
Function Composition - forward composition versus backward compositionFunction Composition - forward composition versus backward composition
Function Composition - forward composition versus backward compositionPhilip Schwarz
 
Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3Philip Schwarz
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Philip Schwarz
 
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
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaPhilip Schwarz
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaSierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaPhilip Schwarz
 

What's hot (20)

Monad Fact #2
Monad Fact #2Monad Fact #2
Monad Fact #2
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’ Addendum to ‘Monads do not Compose’
Addendum to ‘Monads do not Compose’
 
Kleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelationKleisli composition, flatMap, join, map, unit - implementation and interrelation
Kleisli composition, flatMap, join, map, unit - implementation and interrelation
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
 
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'
 
Functor Laws
Functor LawsFunctor Laws
Functor Laws
 
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
 
Scala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data TypeScala 3 enum for a terser Option Monad Algebraic Data Type
Scala 3 enum for a terser Option Monad Algebraic Data Type
 
Function Composition - forward composition versus backward composition
Function Composition - forward composition versus backward compositionFunction Composition - forward composition versus backward composition
Function Composition - forward composition versus backward composition
 
Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
 
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...
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaSierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
 

Similar to Functors

Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsDebasish Ghosh
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsVasil Remeniuk
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Monad as functor with pair of natural transformations
Monad as functor with pair of natural transformationsMonad as functor with pair of natural transformations
Monad as functor with pair of natural transformationsPhilip Schwarz
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverseLuka Jacobowitz
 
Edsc 304 lesson 1
Edsc 304 lesson 1Edsc 304 lesson 1
Edsc 304 lesson 1urenaa
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adtsHang Zhao
 
Lesson 1
Lesson 1Lesson 1
Lesson 1urenaa
 
functions
 functions  functions
functions Gaditek
 
From Function1#apply to Kleisli
From Function1#apply to KleisliFrom Function1#apply to Kleisli
From Function1#apply to KleisliHermann Hueck
 
Lesson 1
Lesson 1Lesson 1
Lesson 1urenaa
 
Function notation by sadiq
Function notation by sadiqFunction notation by sadiq
Function notation by sadiqSadiq Hussain
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1Taisuke Oe
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed worldDebasish Ghosh
 
Functional programming using haskell notes iitk
Functional programming using haskell notes iitkFunctional programming using haskell notes iitk
Functional programming using haskell notes iitkbenevolent001
 

Similar to Functors (20)

Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain Models
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 
haskell_fp1
haskell_fp1haskell_fp1
haskell_fp1
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Monad as functor with pair of natural transformations
Monad as functor with pair of natural transformationsMonad as functor with pair of natural transformations
Monad as functor with pair of natural transformations
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
Edsc 304 lesson 1
Edsc 304 lesson 1Edsc 304 lesson 1
Edsc 304 lesson 1
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
functions
 functions  functions
functions
 
Applicative Functor
Applicative FunctorApplicative Functor
Applicative Functor
 
From Function1#apply to Kleisli
From Function1#apply to KleisliFrom Function1#apply to Kleisli
From Function1#apply to Kleisli
 
Lesson 1
Lesson 1Lesson 1
Lesson 1
 
Function notation by sadiq
Function notation by sadiqFunction notation by sadiq
Function notation by sadiq
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Functional programming using haskell notes iitk
Functional programming using haskell notes iitkFunctional programming using haskell notes iitk
Functional programming using haskell notes iitk
 

More from Philip 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
 
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Philip Schwarz
 

More from Philip Schwarz (20)

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...
 
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
 

Recently uploaded

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
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
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
 
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
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
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
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
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
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
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
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 

Recently uploaded (20)

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
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
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...
 
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
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
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
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
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
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
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...
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 

Functors

  • 1. So this is like the first level of category theory. The next level of category theory is functors. And it is like repeating sort of the same story at a higher level. And category theory is just like this: you make up a story and then you repeat it at some other level. So here we are saying we know what a category is, so suppose we have more than one category, we have several categories. Can we look at a category? Again, we started with sets and we said let’s forget about what is inside a set, let’s just look at connections between sets, now let’s forget what a category is, this is a crazy idea, let’s forget what is inside a category, what are the arrows between categories? We want to define categories using arrows between categories. What’s an arrow between categories? …a category is objects and arrows. So an arrow between categories would map objects to objects and would also map arrows to arrows. That’s a good function on categories. And it turns out that is the beginning of the definition of a functor. A functor is a mapping between categories. Therefore it is a mapping from objects to objects and arrows to arrows. But it cannot just map arrows randomly. If you have an arrow that goes from A to B in one category then A will be mapped into F[A] and B will be mapped to F[B]. It makes sense to map the arrow that goes from A to B to the arrow that goes from F[A] to F[B]. So a functor preserves connections. If there is a connection from A to B in one category then in the target category there will be a connection between F[A] to F[B]. So it just preserves all the connections. Which makes sense. If we want to have a mapping between categories we don’t want to have random mappings, we want to have mappings that are categorical, that preserve the structure. Now let’s go back to programming. What is a functor in programming? First of all, in programming we only have one category…the category of types and functions but that is good enough. We can have a functor that goes from a category to the same category…so in programming we concentrate on these functors that don’t leave the category, they stay in the same category, so the mapping of an object, what does it mean? Objects are types so we have to have a mapping from type to type, which is called a type constructor: you give me a type and I’ll give you a new type. For instance, List is a functor. You give me a type, let’s say Int, and I’ll give you a List[Int]. So there is a mapping that is a type constructor. What about arrows? If I have a function that goes from object A to B, or from type A to type B, so here is this f, I want to map it to an arrow that will go from F[A] to F[B] and this mapping is called fmap. If you have seen a functor in Scala libraries, it has this function called fmap, a higher order function. So if you give me a function that goes from A to B I give you a function that goes from List[A] to List[B]. How is it implemented? Well, it applies the function to every element of the list. So that’s an example of a functor. List is really a functor. It not only maps types to types, but also maps functions to functions. And here is the definition written in Scala. Bartosz Milewski https://twitter.com/BartoszMilewski Bartosz Milewski explaining Functors in category theory as part of his Scala eXchange 2017 Keynote: The Maths Behind Types a functor is a mapping between categories… a mapping from type to type, which is called a type constructor… a mapping from function to function called fmap… e.g. List is a functor. You give me a type, e.g. Int, and I’ll give you a type List[Int]. You give me a function that goes from A to B and I give you a function that goes from List[A] to List[B] https://skillsmatter.com/skillscasts/10179-the-maths-behind-types#video
  • 2. “In functional programming an effect adds some additional capabilities to a computation. And since we are dealing with a statically typed language, these capabilities come in the form of more power from the type system. An effect is modeled usually in the form of a type constructor that constructs types with these additional capabilities. Say we have any type A and we would like to add the capability of aggregation, so that we can treat a collection of A as a separate type. We do this by constructing a type List[A] (for which the corresponding type constructor is List), which adds the effect of aggregation on A. Similarly we can have Option[A] that adds the capability of optionality for the type A.” You can say that a functor abstracts the capability of mapping over a data structure with an ordinary function. Now you may ask what advantage does that bring in the context of domain modeling? The answer is simple – it generalizes your model and makes some parts of your model behavior reusable across components. … A functor gives you the capability to lift a pure function of one argument into a specific context. Have a look at the definition of map in the trait Functor. The pure function f: A => B is lifted into the context of F, transforming F[A] to F[B]. The context here is the type constructor F, also known as the effect of the computation. Have you ever looked at the signatures of map in some of the classes of the Scala standard library like List, Option or Try?...the functions have the same signature if you ignore the context of application. In terms of functional programming we call this context an effect. def map[B](f: (A) => B): List[B] // map for List[A] def map[B](f: (A) => B): Option[B] // map for Option[A] def map[B](f: (A) => B): Try[B] // map for Try[A] List[A] provides an effect of repetition of elements of type A, Option[A] models the effect of uncertainty where the element of type A can either exist or not. Try[A] provides the effect of reification of exceptions. So if you can abstract the effects out of the signatures, then all you have is a function map that lifts the function f into an effect F[_]. Note each of the effects that we are talking about here is modeled using a type constructor – hence F[_] and not a simple F. We can extract this computation structure into a separate trait of its own and call it the Functor. trait Functor[F[_]] { def map[A, B](a: F[A])(f: A => B): F[B] } Since List, Option etc. all share this computational behavior we can make them functors by implementing an interpreter of the functor algebra for each of them. def ListFunctor: Functor[List] = new Functor[List] { def map[A, B](a: List[A])(f: A => B): List[B] = a map f } def OptionFunctor: Functor[Option] = new Functor[Option] { def map[A, B](a: Option[A])(f: A => B): Option[B] = a map f } Debasish Ghosh https://twitter.com/debasishg Debasish Ghosh explaining Functors in his book Functional and Reactive Domain Modeling - https://www.manning.com/books/functional-and-reactive-domain-modeling
  • 3. 4.3.2 Option composition, lifting, and wrapping exception-oriented APIs It may be easy to jump to the conclusion that once we start using Option, it infects our entire code base. One can imagine how any callers of methods that take or return Option will have to be modified to handle either Some or None. But this doesn’t happen, and the reason is that we can lift ordinary functions to become functions that operate on Option. For example, the map function lets us operate on values of type Option[A] using a function of type A => B, returning Option[B]. Another way of looking at this is that map turns a function f of type A => B into a function of type Option[A] => Option[B]. Let’s make this explicit: def lift[A,B](f: A => B): Option[A] => Option[B] = _ map f This tells us that any function that we already have lying around can be transformed (via lift) to operate within the context of a single Option value. Let’s look at an example: val absO: Option[Double] => Option[Double] = lift(math.abs) The math object contains various standalone mathematical functions including abs, sqrt, exp, and so on. We didn’t need to rewrite the math.abs function to work with optional values; we just lifted it into the Option context after the fact. We can do this for any function. EXERCISE 3.18 Write a function map that generalizes modifying each element in a list while maintaining the structure of the list. Here is its signature: def map[A,B](as: List[A])(f: A => B): List[B] Some ‘Functional Programming in Scala’ excerpts explaining Functors https://www.manning.com/books/functional-programming-in-scala by Paul Chiusano and Runar Bjarnason https://twitter.com/runarorama https://twitter.com/pchiusano
  • 4. 11.1 Functors: generalizing the map function In parts 1 and 2, we implemented several different combinator libraries. In each case, we proceeded by writing a small set of primitives and then a number of combinators defined purely in terms of those primitives. We noted some similarities between derived combinators across the libraries we wrote. For instance, we implemented a map function for each data type, to lift a function taking one argument “into the context of” some data type. For Gen, Parser, and Option, the type signatures were as follows: def map[A,B](ga: Gen[A])(f: A => B): Gen[B] def map[A,B](pa: Parser[A])(f: A => B): Parser[B] def map[A,B](oa: Option[A])(f: A => B): Option[B] These type signatures differ only in the concrete data type (Gen, Parser, or Option ). We can capture as a Scala trait the idea of “a data type that implements map”: trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B): F[B] } Here we’ve parameterized map on the type constructor, F[_], much like we did with Foldable in the previous chapter. Instead of picking a particular F[_], like Gen or Parser, the Functor trait is parametric in the choice of F. Here’s an instance for List : val listFunctor = new Functor[List] { def map[A,B](as: List[A])(f: A => B): List[B] = as map f } We say that a type constructor like List (or Option, or F ) is a functor, and the Functor[F] instance constitutes proof that F is in fact a functor. What can we do with this abstraction? As we did in several places throughout this book, we can discover useful functions just by playing with the operations of the interface, in a purely algebraic way… 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. … For Functor , we’ll stipulate the familiar law we first introduced in chapter 7… 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 ! https://www.manning.com/books/functional-programming-in-scala by Paul Chiusano and Runar Bjarnason https://twitter.com/runarorama https://twitter.com/pchiusano
  • 5. What’s a functor? A functor is a way to apply a function over or around some structure that we don’t want to alter. That is, we want to apply the function to the value that is “inside” some structure and leave the structure alone. That’s why it is most common to introduce functor by way of fmapping over lists, as we did back in the Lists chapter. The function gets applied to each value inside the list, and the list structure remains. A good way to relate “not altering the structure” to lists is that the length of the list after mapping a function over it will always be the same. No elements are removed or added, only transformed. Monads sort of steal the Haskell spotlight, but you can do more with Functor and Applicative than many people realize. Also, understanding Functor and Applicative is important to a deep understanding of Monad. The definition of the Functor typeclass in Haskell looks like this: class Functor f where fmap :: (a -> b) -> f a -> f b Functor Laws Instances of the Functor typeclass should abide by two basic laws. Understanding these laws is critical for understanding Functor and writing typeclass instances that are composable and easy to reason about. Identity The first law is the law of identity: fmap id == id If we fmap the identity function, it should have the same result as passing our value to identity. We shouldn’t be changing any of the outer structure 𝑓 that we’re mapping over by mapping id. Composition The second law for Functor is the law of composition: fmap (f . g) == fmap f . fmap g This concerns the composability of fmap. If we compose two functions, 𝑓 and 𝑔, and fmap that over some structure, we should get the same result as if we fmapped them and then composed them… If an implementation of fmap doesn’t do that, it’s a broken functor. http://haskellbook.com/ By Christopher Allen, Julie Moronuki https://twitter.com/bitemyapp https://twitter.com/argumatronic Some ‘Haskell Programming from First Principles’ excerpts explaining Functors