Skip to content

Instantly share code, notes, and snippets.

@bpinedah
Last active December 11, 2018 22:29
Show Gist options
  • Save bpinedah/b11eefe351e37db787237df107943748 to your computer and use it in GitHub Desktop.
Save bpinedah/b11eefe351e37db787237df107943748 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;
/**
* @description Compose function to compose functions
* @param fns Functions to reduce on composition
* @returns {Function} Compose function
*/
const compose = (...fns) => x => fns.reduceRight((f, m) => m(f), x);
// Age and lastname compose function to apply changes
compose(
map(applier('age', 33)),
filter(where('name', 'Bruce'))
)(items);
compose(
map(applier('lastname', 'Roque')),
filter(where('name', 'Fabs'))
)(items);
// Print changes
console.log(items);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment