Sunday, 25 June 2017

Functional Definitions: Monoid, Functor, Monad


Monoid


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


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]
}

Monad




No comments:

Post a Comment

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning...