Skip to content

Instantly share code, notes, and snippets.

@bpinedah
Created December 10, 2018 22:11
Show Gist options
  • Save bpinedah/3be0f4411919e700f235943e751f07de to your computer and use it in GitHub Desktop.
Save bpinedah/3be0f4411919e700f235943e751f07de to your computer and use it in GitHub Desktop.
Currying article
// Data example
const items = [{name: 'Bruce' }, {name: 'Fabs'}, {name: 'Bruce'}, {name: 'Gaby'}];
/**
* @description Filter by one where condition
* @param w Where condition function
* @returns {Function} filter function
*/
const filter = w => a => a.filter(w);
/**
* @description Create a where condition function
* @param k Criteria key name
* @param v Criteria value
* @returns {Function} Where condition function
*/
const where = (k, v) => i => i[k] === v;
/**
* @description Create a map function
* @param fnMap Function to set on map callback
* @returns {Function} Map function
*/
const map = fnMap => f => f.map(fnMap);
/**
* @description Create a modification function
* @param k Key to set any value
* @param v Value to set
* @returns {Function} Modification function
*/
const applier = (k, v) => x => x[k] = v;
// Apply the modification
map(applier('lastname', 'Wayne'))(filter(where('name', 'Bruce'))(items));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment