Skip to content

Instantly share code, notes, and snippets.

@chayleaf
Last active December 4, 2025 19:15
Show Gist options
  • Select an option

  • Save chayleaf/118b53d9e78a2389abb00ed011c9fa30 to your computer and use it in GitHub Desktop.

Select an option

Save chayleaf/118b53d9e78a2389abb00ed011c9fa30 to your computer and use it in GitHub Desktop.
class Maybe {
bind(f) {
return this({
Some: f,
None: () => None,
});
}
map(f) {
return this({
Some: x => Some(f(x)),
None: () => None,
});
}
unwrap() {
return this({
Some: x => x,
None: () => { throw new Exception("something something error message"); },
});
}
}
function Some(val) {
const ret = x => x.Some(val);
Object.setPrototypeOf(ret, Some.prototype);
return ret;
}
Object.setPrototypeOf(Some.prototype, Maybe.prototype);
function _None() {
const ret = x => x.None();
Object.setPrototypeOf(ret, _None.prototype);
return ret;
}
Object.setPrototypeOf(_None.prototype, Maybe.prototype);
const None = _None();
None.prototype = _None.prototype;
console.log(Some(5).map(x => x + 1).unwrap());
console.log(Some(5) instanceof Maybe, None instanceof Maybe);
console.log(Some(5) instanceof Some, None instanceof Some);
console.log(Some(5) instanceof None, None instanceof None);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment