Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Functors, in theory
and in practice !
Me, myself and I
●
●
●
●
●
●
Agenda
●
●
●
●
●
An intuition
An intuition
-
In practice ...
A common pattern
def isMagic(x: Int): Boolean = x == 42
A common pattern
val intO: Option[Int] = Some(42)
val res: Option[Boolean] = map(intO, isMagic)
A common pattern
val intL: List[Int] = List(1, 42, 99)
val res: List[Boolean] = map(intL, isMagic)
A common pattern
case class Container[A](value: A)
val intC: Container[Int] = Container(42)
val res: Container[Boolean] = ...
A common pattern
def map[F[_], A, B](ca: F[A], f: A => B): F[B]
F[A], (A => B) => F[B]
A common pattern
A type class - Definition
A type class - Definition
A type class - An exemple
trait Show[A] {
def print(a: A): String
}
A type class - An exemple
implicit val intShow: Show[Int] = new Show[Int] {
def show(a: Int): String = s"I'm an Int: $a"
}
A type class - Functor
trait Functor[F[_]] {
def map[A, B](fa: F[A], fab: A => B): F[B]
}
A type class - Type Constructors
A type class - Functor
trait Functor[F[_]] {
def map[A, B](fa: F[A], fab: A => B): F[B]
}
A type class - Functor
implicit val cFunctor = new Functor[Container] {
def map[A, B](fa: Container[A], fab: A => B): Cont...
A type class - Functor
… in theory ...
Category theory
Category theory
Category theory
-
Category theory
How does it relate to Scala ?
How does it relate to Scala ?
How does it relate to Scala ?
def identity[A](a: A): A = a
How does it relate to Scala ?
Back to our functors !
Back to our functors !
Back to our functors !
Objects dans S (Scal) Objects in S’ (Scal too)
A Option[A]
Int Option[Int]
String Option[String]
Back to our functors !
Back to our functors !
Morphisms of S (Scal) Morphisms of S’ (Scal too)
A → A (identity) Option[A] → Option[A]
A → B Optio...
… and back to
practice again !
What does it bring us ?
def cantDoMore[F[_]: Functor, A](fa: F[A]) = fa.map(...)
What does it bring us ?
-
Conclusion
Functors
-
Further reading
Thanks !
(easy) Questions ?
Upcoming SlideShare
Loading in …5
×

Functors, in theory and in practice

960 views

Published on

flatMap Oslo 2019 presentation

Published in: Software
  • Be the first to comment

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 - Definition
  14. 14. A type class - Definition
  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 -
  41. 41. Further reading
  42. 42. Thanks ! (easy) Questions ?

×