Last active
May 15, 2016 18:56
-
-
Save rpominov/e554f67a6b0d7f39401a243d24caf535 to your computer and use it in GitHub Desktop.
An idea of Algebraic data types helper in JS
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
const Maybe = Type({Just: ['value'], Nothing: []}, { | |
map(fn, maybe) { | |
return Maybe.case({ | |
Just: ({value}) => Maybe.Just(fn(value)), | |
// Should x => x be the default handler in case() so we could omit Nothing here? | |
Nothing: m => m, | |
}) | |
} | |
}) | |
const just1 = Maybe.Just(1) // {T: 'Just', value: 1} | |
const nothing = Maybe.Nothing() // {T: 'Nothing'} | |
Maybe.map(x => x + 1, just1) // {T: 'Just', value: 2} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Similar stuff: