Skip to content

Instantly share code, notes, and snippets.

@zhangchiqing
Last active March 30, 2016 20:05
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 zhangchiqing/e4179760a0a7402974995afe9f4e7501 to your computer and use it in GitHub Desktop.
Save zhangchiqing/e4179760a0a7402974995afe9f4e7501 to your computer and use it in GitHub Desktop.
// 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