Skip to content

Instantly share code, notes, and snippets.

@AZaviruha
Created February 17, 2015 15:32
Show Gist options
  • Save AZaviruha/7f743d82502d2d0889f6 to your computer and use it in GitHub Desktop.
Save AZaviruha/7f743d82502d2d0889f6 to your computer and use it in GitHub Desktop.
function Maybe ( val ) {
this.__val = val;
this.__error = this.isError();
this.__defaultVal = null;
}
Maybe.prototype.isError = function () {
var val = this.__val;
return (null === val)
|| (undefined === val)
|| (Infinity === val)
};
Maybe.prototype.mBind = function ( f ) {
var val = this.__val;
if ( this.isError() ) {
this.__val = null;
this.__error = true;
return this;
}
try {
this.__val = f( val );
}
catch ( e ) {
this.__val = null;
this.__error = true;
console.log( 'Maybe error :: ', e );
}
finally {
return this;
}
};
Maybe.prototype.mDefault = function ( val ) {
this.__defaultVal = val;
return this;
};
Maybe.prototype.value = function () {
return this.__error ? this.__defaultVal : this.__val;
};
@AZaviruha
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment