Skip to content

Instantly share code, notes, and snippets.

@KEIII
Last active April 19, 2023 16:31
Show Gist options
  • Save KEIII/fbaee8b28f5cfe78842ab947065caefe to your computer and use it in GitHub Desktop.
Save KEIII/fbaee8b28f5cfe78842ab947065caefe to your computer and use it in GitHub Desktop.
Functor & Monad type
/**
* A functor is simply something that can be mapped over
*/
type Functor<A> = {
unit: (x: A) => Functor<A>;
map: <B>(f: (x: A) => B) => Functor<B>; // aka fmap
}
/**
* For a monad `M`, a value of type M<A> represents having access
* to a value of type `A` within the context of the monad
*/
type Monad<A> = {
/**
* Type constructor
*
* Wraps any value of type A into a monadic value of type M<A>
*
* return :: a -> m a
*/
unit: (x: A) => Monad<A>; // aka of | pure
/**
* Composition
*
* Maps the monadic type M<A> to the monadic type M<B>
* given a monadic function of type A -> M<B>
*
* bind :: M a -> (a -> M b) -> M b
*/
flatMap: <B>(f: (x: A) => Monad<B>) => Monad<B>; // aka >>= | bind | chain
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment