A practical introduction to Category Theory - ScalaItaly 2017

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.

E99b07644586e9e1723757bf8e34ea68?s=128

Daniela Sfregola

May 13, 2017
Tweet

Transcript

  1. 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
  2. 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) } }
  3. 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 }
  4. 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 } }
  5. 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
  6. 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
  7. 35.

    Band BoundedSemilattice CommutativeGroup CommutativeMonoid CommutativeSemigroup Eq Group Semigroup Monoid Order

    PartialOrder Semilattice Alternative Applicative AppllicativeError Apply Bifoldable Bimonad Bitraverse Cartesian CoflatMap 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.
  8. 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