Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- operator (>>=) 1 left { $a, $b } => #{ $a.bind($b) }
- operator (>>) 1 left { $a, $b } => #{ $a.bind((_) => $b) }
- function Just(value) {
- this.value = value;
- }
- Just.prototype.bind = function(fn) {
- return fn(this.value)
- }
- Just.prototype.toString = function() {
- return "Just(" + this.value + ")"
- }
- function Nothing(value) {}
- Nothing.prototype.bind = function(_) {
- return this
- }
- Nothing.prototype.toString = function() {
- return "Nothing()"
- }
- function sqrtMaybe(x) {
- return x >= 0 ? new Just(Math.sqrt(x)) : new Nothing()
- }
- const result = sqrtMaybe(25)
- >>= (x) => sqrtMaybe(36)
- >>= (y) => new Just(x + y)
- alert(result) // => Just(11)
- const result2 = sqrtMaybe(-1)
- >>= (x) => alert("This code won't be executed")
- alert(result2) // => Nothing()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement