Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Last active March 2, 2019 00:25
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 ehzawad/a4b1e7f85a8961c5d620ffa179e5d1ca to your computer and use it in GitHub Desktop.
Save ehzawad/a4b1e7f85a8961c5d620ffa179e5d1ca to your computer and use it in GitHub Desktop.
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)
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