Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A practical introduction to Category Theory - S...

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.

Daniela Sfregola

May 13, 2017
Tweet

More Decks by Daniela Sfregola

Other Decks in Programming

Transcript

  1. 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. 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. 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. 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. 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. 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. 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. WANNA KNOW MORE? > Category Theory for the WH by

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