Last active
March 30, 2016 20:05
-
-
Save zhangchiqing/e4179760a0a7402974995afe9f4e7501 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
// Imperative | |
function foo(bar) { | |
if (!bar) { | |
return ; | |
} | |
return bar + 1; | |
} | |
foo(null); | |
// > null | |
foo(1); | |
// 2 | |
// Functional | |
function _foo(bar) { | |
return bar + 1; | |
} | |
var foo = fmap(_foo); | |
foo(Nothing) | |
// > Nothing | |
foo(Just(1)) | |
// > Just 2 | |
// Bad | |
function findBar(q) { | |
return findOne(q).then(function(bar) { | |
if (!bar) { | |
return; | |
} | |
return bar + 1; | |
}); | |
} | |
// Good | |
function _foo(bar) { | |
return bar + 1; | |
} | |
// q -> Promise Maybe bar | |
function findBar(q) { | |
return findOne(q).then(fromMaybe(_foo)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment