Reference: https://bartoszmilewski.com/2015/01/20/functors/
Reference: https://typelevel.org/cats/typeclasses/functor.html
Basic info about functors:
- https://github.com/mtumilowicz/java11-category-theory-optional-is-not-functor
- https://github.com/mtumilowicz/java11-category-theory-reader-functor
We have optionList:Option[List[Int]] = Option(List(1, 2, 3))
and we want to square the elements.
- pure Scala
def square(i: Int) = i * i val optionList = Option(List(1, 2, 3)) optionList.map(_ . map(square)) should be (Some(List(1, 4, 9)))
- Scala + Cats
- Option is a functor
- List is a functor
- Composition of two functors, when acting on objects, is just the composition of their respective object mappings.
def square(i: Int) = i * i val optionList = Option(List(1, 2, 3)) Functor[Option].compose[List].map(optionList)(square) should be (Some(List(1, 4, 9)))