Skip to content

Instantly share code, notes, and snippets.

@mlms13
Last active January 29, 2020 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlms13/3b22abda2981b5f5e9360caea43f947d to your computer and use it in GitHub Desktop.
Save mlms13/3b22abda2981b5f5e9360caea43f947d to your computer and use it in GitHub Desktop.
Reader, I guess
let const = (a, _) => a;
let (<<) = (f, g, a) => f(g(a));
module type Type = {type t;};
module Function1 = {
type t('input, 'output) = 'input => 'output;
};
module type Functor = {
type t('a);
let map: ('a => 'b, t('a)) => t('b);
};
module type Apply = {
include Functor;
let liftA2: (('a, 'b) => 'c, t('a), t('b)) => t('c);
};
module type Applicative = {
include Apply;
let pure: 'a => t('a);
};
module type Monad = {
include Applicative;
let flatMap: ('a => t('b), t('a)) => t('b);
};
module Reader = (Input: Type) : Monad => {
type t('a) = Function1.t(Input.t, 'a);
let map = (<<);
let liftA2 = (f, a, b, input) => f(a(input), b(input));
let pure = const;
let flatMap = (f, a, input) => f(a(input), input);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment