Created
February 17, 2015 15:32
-
-
Save AZaviruha/7f743d82502d2d0889f6 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
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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: http://jsfiddle.net/azaviruha/pusn0zLL/15/