Daniela Sfregola
May 13, 2017
1.1k

# A practical introduction to Category Theory - ScalaItaly 2017

Category Theory has become one of the hot topics in our community. Why is this theory suddenly so interesting for developers? Why are the cool kids talking so much about it? How can we apply its principles in our code?

This talk will introduce the general principles behind Category Theory, it will show practical examples of how this theory has managed to simplify and solve common challenges that we encounter in our code daily.

May 13, 2017

## Transcript

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

am not a mathematician !

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

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

mathematical laws

18. ### MONOID trait Monoid[A] { def combine(x: A, y: A): A

def empty: A }

21. ### EXAMPLE OF CONTAINERS > Option > Future > Try >

List > Either

23. ### FUNCTOR class Functor[Box[_]] { def map[A, B](value: Box[A]) (f: A

=> B): Box[B] }
24. ### EXAMPLE OF FUNCTOR scala> Some("daniela").map(s => "yo " + s)

res0: Option[String] = Some(yo daniela) scala> None.map(s => Some("yo " + s)) res1: Option[String] = None

26. ### 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 map[A, B](value: Box[A])(f: A => B): Box[B] = { val boxF = pure(f) ap(boxF)(value) } }

28. ### 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 }

31. ### MONAD class Monad[Box[_]] extends Applicative[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 } }

33. ### 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
34. ### 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
35. ### Band BoundedSemilattice CommutativeGroup CommutativeMonoid CommutativeSemigroup Eq Group Semigroup Monoid Order

PartialOrder Semilattice Alternative Applicative AppllicativeError Apply Bifoldable Bimonad Bitraverse Cartesian CoﬂatMap Comonad ContravariantCartesian FlatMap Foldable Functor FunctorFilter Inject InvariantMonoidal Monad MonadCombine MonadError MonoidK NotNull Reducible SemigroupK Show TransLift TraverseFilter Trivial Unapply Bifunctor Contravariant Invariant Profunctor Strong Traverse MonadFilter MonadReader MonadState MonadWriter Arrow Category Choice Compose Split Cats Type Classes kernel core/functor core/arrow core The highlighted type classes are the first ones you should learn. They’re well documented and well-known so it’s easy to get help. a |+| b a === b a =/= b a |@| b a *> b a <* b a <+> b a >>> b a <<< b a > b a >= b a < b a <= b Sync Async Effect LiftIO core MTL effect Some type classes introduce symbolic operators.

37. ### WANNA KNOW MORE? > Category Theory for the WH by

@PhilipWadler > Category Theory by @BartoszMilewski > Cats-Infographics by Rob Norris - @tpolecat > Cats Documentation - Type Classes