Daniela Sfregola
September 19, 2017
2.1k

# ScalaWorld 2017 - A Pragmatic Introduction to Category Theory

## Daniela Sfregola

September 19, 2017

## Transcript

1. ### A PRAGMATIC INTRODUCTION TO CATEGORY THEORY @DANIELASFREGOLA SCALA WORLD 2017

github.com/DanielaSfregola/tutorial-cat

3. ### HELLOOOOO > ex Java Developer > OOP background > I

am not a mathematician !

SCALA CODE
6. ### YOU DO NOT NEED TO KNOW CATEGORY THEORY TO WRITE

FUNCTIONAL CODE

10. ### OBJECT ORIENTED PROGRAMMING (OOP) > composition of objects > abstraction

over the behaviour of each object
11. ### FUNCTIONAL PROGRAMMING (FP) > composition of functions > abstraction over

mathematical laws

22. ### MONOID'S RULES Identity n o id == id o n

== n Composition forall x, y => x o y Associativity x o (y o z) == (x o y) o z

y: A): A }
25. ### EXERCISES ON MONOID > Define a monoid for Int >

Define a monoid for String sbt 'testOnly *Monoid*' github.com/DanielaSfregola/tutorial-cat

28. ### CATEGORY IN A BOX > Objects are in a Box

> All the arrows are copied

30. ### EXAMPLE OF BOXES > Option > Future > Try >

List > Either

32. ### FUNCTOR'S RULES Identity map(id) == id Composition map(g o f)

== map(g) o map(f) Associativity map(h o g) o map(f) == map(h) o map(g o f)

the context
35. ### FUNCTOR class Functor[Box[_]] { def map[A, B](boxA: Box[A]) (f: A

=> B): Box[B] }
36. ### EXERCISES ON FUNCTOR > Define a functor for Maybe >

Define a functor for ZeroOrMore sbt 'testOnly *Functor*' github.com/DanielaSfregola/tutorial-cat

...and more!
40. ### COMBINE BOXES TOGETHER > How to create a new box

> How to combine their values together
41. ### APPLICATIVE class Applicative[Box[_]] extends Functor[Box] { def pure[A](a: A): Box[A]

def ap[A, B](boxF: Box[A => B])(boxA: Box[A]): Box[B] def map[A, B](boxA: Box[A])(f: A => B): Box[B] = ??? // spoiler alert }

43. ### APPLICATIVE class Applicative[Box[_]] extends Functor[Box] { def pure[A](a: A): Box[A]

def ap[A, B](boxF: Box[A => B])(value: Box[A]): Box[B] def ap2[A1, A2, B](boxF: Box[(A1, A2) => B]) (value1: Box[A1], value2: Box[A2]): Box[B] // up to 22 values! // same for map }
44. ### EXERCISES ON APPLICATIVE > Define map in terms of ap

and pure > Define an applicative for Maybe > Define an applicative for ZeroOrMore sbt 'testOnly *Applicative*' github.com/DanielaSfregola/tutorial-cat

49. ### MONAD (AS FUNCTOR) class Monad[Box[_]] extends Functor[Box] { def flatten[A](bb:

Box[Box[A]]): Box[A] def flatMap[A, B](valueA: Box[A]) (f: A => Box[B]): Box[B] = { val bb: Box[Box[B]] = map(valueA)(f) bb.flatten } }

51. ### FOR-COMPREHENSION val boxA: Box[A] def toBoxB: A => Box[B] def

toBoxC: B => Box[C] def toBoxD: C => Box[D] for { a <- boxA b <- toBoxB(a) c <- toBoxC(b) d <- toBoxD(c) } yield d
52. ### MONAD (AS APPLICATIVE) trait Monad[Box[_]] extends Applicative[Box] { def flatMap[A,

B](boxA: Box[A])(f: A => Box[B]): Box[B] // TODO - implement using flatMap def flatten[A](boxBoxA: Box[Box[A]]): Box[A] = ??? // TODO - implement using flatMap and map override def ap[A, B](boxF: Box[A => B])(boxA: Box[A]): Box[B] = ??? // TODO - implement using flatMap and pure override def map[A, B](boxA: Box[A])(f: A => B): Box[B] = ??? }
53. ### EXERCISES ON MONAD (1) > Define flatten using flatMap >

Define map using flatMap and pure > Define ap using flatMap and map github.com/DanielaSfregola/tutorial-cat

57. ### SUMMARY CATEGORY THEORY >> how things compose MONOID >> combining

2 values into 1 FUNCTOR >> values lifted to a context APPLICATIVE >> independent values applied to a function in a context MONAD >> ops in sequence in a context