Successfully reported this slideshow.

×

Functors, in theory and in practice

flatMap Oslo 2019 presentation

flatMap Oslo 2019 presentation

More Related Content

Functors, in theory and in practice

1. 1. Functors, in theory and in practice !
2. 2. Me, myself and I ● ● ● ● ● ●
3. 3. Agenda ● ● ● ● ●
4. 4. An intuition
5. 5. An intuition -
6. 6. In practice ...
7. 7. A common pattern def isMagic(x: Int): Boolean = x == 42
8. 8. A common pattern val intO: Option[Int] = Some(42) val res: Option[Boolean] = map(intO, isMagic)
9. 9. A common pattern val intL: List[Int] = List(1, 42, 99) val res: List[Boolean] = map(intL, isMagic)
10. 10. A common pattern case class Container[A](value: A) val intC: Container[Int] = Container(42) val res: Container[Boolean] = map(intC, isMagic)
11. 11. A common pattern def map[F[_], A, B](ca: F[A], f: A => B): F[B] F[A], (A => B) => F[B]
12. 12. A common pattern
13. 13. A type class - Deﬁnition
14. 14. A type class - Deﬁnition
15. 15. A type class - An exemple trait Show[A] { def print(a: A): String }
16. 16. A type class - An exemple implicit val intShow: Show[Int] = new Show[Int] { def show(a: Int): String = s"I'm an Int: \$a" }
17. 17. A type class - Functor trait Functor[F[_]] { def map[A, B](fa: F[A], fab: A => B): F[B] }
18. 18. A type class - Type Constructors
19. 19. A type class - Functor trait Functor[F[_]] { def map[A, B](fa: F[A], fab: A => B): F[B] }
20. 20. A type class - Functor implicit val cFunctor = new Functor[Container] { def map[A, B](fa: Container[A], fab: A => B): Container[B] = Container(fab(fa.value)) }
21. 21. A type class - Functor
22. 22. … in theory ...
23. 23. Category theory
24. 24. Category theory
25. 25. Category theory -
26. 26. Category theory
27. 27. How does it relate to Scala ?
28. 28. How does it relate to Scala ?
29. 29. How does it relate to Scala ? def identity[A](a: A): A = a
30. 30. How does it relate to Scala ?
31. 31. Back to our functors !
32. 32. Back to our functors !
33. 33. Back to our functors ! Objects dans S (Scal) Objects in S’ (Scal too) A Option[A] Int Option[Int] String Option[String]
34. 34. Back to our functors !
35. 35. Back to our functors ! Morphisms of S (Scal) Morphisms of S’ (Scal too) A → A (identity) Option[A] → Option[A] A → B Option[A] → Option[B] Int → String Option[Int] → Option[String]
36. 36. … and back to practice again !
37. 37. What does it bring us ? def cantDoMore[F[_]: Functor, A](fa: F[A]) = fa.map(...)
38. 38. What does it bring us ? -
39. 39. Conclusion
40. 40. Functors -