Last active
February 25, 2020 20:52
-
-
Save cowboy/c5332087e46d857a6153365157727073 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 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