Skip to content

Instantly share code, notes, and snippets.

@chriswrightdesign
Last active June 23, 2017 05:07
Show Gist options
  • Save chriswrightdesign/a1bff07e438786bc196d6908945ec8f7 to your computer and use it in GitHub Desktop.
Save chriswrightdesign/a1bff07e438786bc196d6908945ec8f7 to your computer and use it in GitHub Desktop.
Map closure
// Make our function useful within a mapping function:
const prefix = prefix => value => `${prefix}${value}`;
// our function takes a prefix string, then returns a function which takes value,
// which then returns a string with the prefix and value together.
const animals = ['dog', 'cat', 'bird', 'giraffe'];
animals.map(prefix('super '); // result: ['super dog', 'super cat', 'super bird'];
// Alternatively, we could also name our outer function for clarity:
const prefixSuper = prefix('super ');
animals.map(prefixSuper); // result: ['super dog', 'super cat', 'super bird'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment