Last active
December 4, 2025 19:15
-
-
Save chayleaf/118b53d9e78a2389abb00ed011c9fa30 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
| 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