Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 1.22 KB

File metadata and controls

32 lines (27 loc) · 1.22 KB

Build Status

scala212-cats-category-theory-composing-functors

Reference: https://bartoszmilewski.com/2015/01/20/functors/
Reference: https://typelevel.org/cats/typeclasses/functor.html

preface

Basic info about functors:

project description

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)))