Skip to content

mtumilowicz/scala212-cats-category-theory-composing-functors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simple example of composing functors using Cats.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages