-
-
Save hughfdjackson/5141769 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
var peeps = { hugh: { name: 'hugh jackson' } } | |
var titleCase = function(str){ | |
return str.split(' ').map(function(str){ return str.substr(0, 1).toUpperCase() + str.substr(1) }).join(' ') | |
} | |
var val = maybe(peeps) | |
.map(function(peeps){ return peeps.tahir }) | |
.map(function(peep){ return peep.name }) | |
.map(titleCase) | |
.getOrElse('couldn\'t find person') | |
val //= couldn't find person | |
var val2 = maybe(peeps) | |
.map(function(peeps){ return peeps.hugh }) | |
.map(function(peep){ return peep.name }) | |
.map(titleCase) | |
.getOrElse('couldn\'t find person') | |
val2 //= Hugh Jackson |
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(value) { | |
var obj = null; | |
function isEmpty() { return value === undefined || value === null } | |
function nonEmpty() { return !isEmpty() } | |
obj = { | |
map: function (f) { return isEmpty() ? obj : maybe(f(value)) }, | |
getOrElse: function (n) { return isEmpty() ? n : value }, | |
isEmpty: isEmpty, | |
nonEmpty: nonEmpty | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment