Skip to content

mtumilowicz/scala212-category-theory-kleisli-writer-category-functor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

scala212-category-theory-kleisli-writer-category-functor

Reference: https://bartoszmilewski.com/2015/02/03/functoriality/

preface

Please refer my other github repository:

project description

  1. We have simple type constructor Writer
    class Writer[X](val result: X, val log: String) {}
    
  2. And its Kleisli Category
    object KleisliWriterCategory {
      def identity[A]: Function[A, Writer[A]] = new Writer(_, "")
    
      def compose[A, B, C](f1: Function[A, Writer[B]], f2: Function[B, Writer[C]]): Function[A, Writer[C]] = (a: A) => {
        val f1value = f1.apply(a)
        val f2value = f2.apply(f1value.result)
        new Writer(f2value.result, f1value.log + f2value.log)
      }
    }
    
    • function compose is >=>
  3. We could easily lift type constructor Writer to the functor, by implementing the map function
    class Writer[X](val result: X, val log: String) {
      def map[Y](f: Function[X, Y]): Writer[Y] = 
        KleisliWriterCategory.compose[Writer[X], X, Y](identity, KleisliWriterCategory.identity.compose(f))(this)
    }
    

Notice that (3.) is very general: you can replace Writer with any type constructor. As long as it supports a fish operator (Kleisli Operator) and the identity function you can define map as well.

About

Constructing functor using Kleisli operator.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages