-
-
Save rightfold/0644f73fa0ceeca383e7 to your computer and use it in GitHub Desktop.
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[_]] { | |
def map[T, U](function: T => U)(functor: F[T]): F[U] | |
} | |
trait Applicative[F[_]] extends Functor[F] { | |
def pure[T](value: T): F[T] | |
def seq[T, U](function: F[T => U])(functor: F[T]): F[U] | |
override def map[T, U](function: T => U)(functor: F[T]): F[U] = | |
seq(pure(function))(functor) | |
} | |
trait Monad[F[_]] extends Applicative[F] { | |
def bind[T, U](functor: F[T])(function: T => F[U]): F[U] | |
override def seq[T, U](function: F[T => U])(functor: F[T]): F[U] = | |
bind(function) { bfunction => bind(functor) { bfunctor => pure(bfunction(bfunctor)) } } | |
} | |
object ListInstance extends Monad[List] { | |
// only have to implement pure and bind; map and seq are derived | |
override def pure[T](value: T) = List(value) | |
override def bind[T, U](functor: List[T])(function: T => List[U]) = | |
functor flatMap function | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment