Monoid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Monoid[A] { | |
// a monoid comprises an associative binary operator ... | |
def op(a1: A, a2: A): A | |
// ... and a 'zero' value such that op(x, zero) == x | |
// and op(zero, x) = x | |
def zero: A | |
} | |
Object Monoid { | |
// integer addition is monoidal | |
val intAddition: Monoid[Int] = new Monoid[Int] { | |
def op(x: Int, y: Int) = x + y | |
val zero = 0 | |
} | |
} |
Functor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Functor[F[_]] { | |
// a functor is just a thing with a map function | |
def map[A, B](a: F[A])(f: A => B): F[B] | |
} |
No comments:
Post a Comment