Created
August 31, 2018 07:25
-
-
Save piq9117/12ebb6e8793ea80517835ccbd158a537 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type identity('a) = Identity('a); | |
module Functor_class = { | |
module type F = { | |
type t(_); | |
let map: ('a => 'b, t('a)) => t('b); | |
}; | |
}; | |
module Applicative_class = { | |
include Functor_class; | |
module type A = { | |
type t(_); | |
let pure: 'a => t('a); | |
let ap: (t('a => 'b), t('a)) => t('b); | |
} | |
} | |
module Monad_class = { | |
include Applicative_class; | |
module type M = { | |
type t(_); | |
let return: 'a => t('a); | |
let bind: ('a => t('b), t('a)) => t('b); | |
} | |
} | |
module Identity_functor: Functor_class.F with type t('a) = identity('a) = { | |
type t('a) = identity('a); | |
let map = (f) => | |
fun | Identity(a) => Identity(f(a)); | |
} | |
module Identity_applicative: Applicative_class.A with type t('a) = identity('a) = { | |
type t('a) = identity('a); | |
let pure = a => Identity(a); | |
let ap = (fab, fa) => | |
switch(fab, fa) { | |
| (Identity(f), Identity(g)) => Identity(f(g)); | |
} | |
}; | |
module Identity_monad: Monad_class.M with type t('a) = identity('a) = { | |
type t('a) = identity('a); | |
let return = a => Identity(a); | |
let bind = f => | |
fun | Identity(a) => f(a); | |
}; | |
let functorResult = Identity(3) |> Identity_functor.map(n => n + 2); | |
let applicativeResult = Identity(3) |> Identity_applicative.ap(Identity(n => n + 2)); | |
let monadResult = Identity(3) |> Identity_monad.bind(n => Identity(n + 2)) | |
Js.log(functorResult); /* [ 5 ] */ | |
Js.log(applicativeResult); | |
Js.log(monadResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment