Last active
May 9, 2020 09:11
-
-
Save zaetrik/1ab0d206d0f55da7c5e70c9cbde80894 to your computer and use it in GitHub Desktop.
Applicative of() example
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
// This is how fp-ts implements this for Option | |
// Taken from https://github.com/gcanti/fp-ts/blob/master/src/Option.ts | |
interface None { | |
readonly _tag: "None"; | |
} | |
interface Some<A> { | |
readonly _tag: "Some"; | |
readonly value: A; | |
} | |
type Option<A> = None | Some<A>; | |
function some<A>(a: A): Option<A> { | |
return { _tag: "Some", value: a }; | |
} | |
const OptionApplicative = { | |
map: (ma, f) => (isNone(ma) ? none : some(f(ma.value))), | |
of: some, | |
ap: (mab, ma) => (isNone(mab) ? none : isNone(ma) ? none : some(mab.value(ma.value))), | |
} | |
// Now we can create a new instance like this => | |
const MyApplicative = OptionApplicative.of(5) // { _tag: "Some", value: 5 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment