Skip to content

Instantly share code, notes, and snippets.

@rpominov
Last active May 15, 2016 18:56
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 rpominov/e554f67a6b0d7f39401a243d24caf535 to your computer and use it in GitHub Desktop.
Save rpominov/e554f67a6b0d7f39401a243d24caf535 to your computer and use it in GitHub Desktop.
An idea of Algebraic data types helper in JS
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}
@rpominov
Copy link
Author

@rpominov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment