Last active
March 2, 2019 00:25
-
-
Save ehzawad/a4b1e7f85a8961c5d620ffa179e5d1ca to your computer and use it in GitHub Desktop.
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
var Maybe = function(x) { | |
this.__value = x; | |
} | |
Maybe.of = function(x) { | |
return new Maybe(x) | |
} | |
Maybe.prototype.isNothing = function() { | |
return (this.__value === null || this.__value === undefined) | |
} | |
Maybe.prototype.map = function(f) { | |
return this.isNothing() ? Maybe.of(null) : Maybe.of(f(this.__value)) | |
} | |
const print = (f) => console.log(f) | |
var double = Maybe.of(3).map(function(value) { | |
return value + 3 ; | |
}) | |
print(double) | |
var uppercaser = Maybe.of("Salman The Brown Fish").map(function(val) { | |
return val.toUpperCase() | |
}) | |
print(uppercaser) | |
var noValue = Maybe.of().map(function(value) { | |
return value + 3 ; | |
}) | |
print(noValue) |
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
var Container = function(x) { | |
this.__value = x; | |
} | |
Container.of = function(x) { return new Container(x) } | |
const print = (f) => console.log(f) | |
print(Container.of(3)) | |
// Container of string | |
print(Container.of('pizza')) | |
// Container of Container | |
print(Container.of(Container.of({name: 'Kiki', age: 21}))) | |
// (a -> b) -> Container a -> Container b | |
Container.prototype.map = function(F) { | |
return Container.of(F(this.__value)) | |
} | |
var double = Container.of(3).map(function(value) { | |
return value + 3 ; | |
}) | |
print(double) | |
var uppercaser = Container.of("Salman The Brown Fish").map(function(val) { | |
return val.toUpperCase() | |
}) | |
print(uppercaser) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment