Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active February 25, 2020 20:52
Show Gist options
  • Save cowboy/c5332087e46d857a6153365157727073 to your computer and use it in GitHub Desktop.
Save cowboy/c5332087e46d857a6153365157727073 to your computer and use it in GitHub Desktop.
function getAdder(a) {
return function(b) {
return a + b;
};
}
// Another way to write the same is this:
// const getAdder = a => b => a + b;
const addOne = getAdder(1);
addOne(2) // 3
addOne(5) // 6
const addTen = getAdder(10);
addTen(2) // 12
addTen(5) // 15
// more:
function getErrorFormatter(prefix, suffix) {
return function(error) {
return `${prefix}${error.message}${suffix}`;
};
}
// Another way to write the same is this:
// const getErrorFormatter = (prefix, suffix) => error => `${prefix}${error.message}${suffix}`;
const fooReporter = getErrorFormatter('foo <', '>');
fooReporter(new Error('what')) // foo <what>
const barReporter = getErrorFormatter('bar {', '}');
barReporter(new Error('yo')) // bar {yo}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment