Skip to content

Instantly share code, notes, and snippets.

@Tallies
Last active April 23, 2020 20:04
Show Gist options
  • Save Tallies/cc9c4302ed63ebd6c89fb25641d95a75 to your computer and use it in GitHub Desktop.
Save Tallies/cc9c4302ed63ebd6c89fb25641d95a75 to your computer and use it in GitHub Desktop.
Practical currying in Javascript

Source examples for currying used in the article

https://www.linkedin.com/pulse/practical-currying-javascript-charl-marais

sum.js

A sum functions taking to arguments and returning the sum.

sum-currying.js

A sum function taking one argument and returning function that takes an argument and return the sum of the first with second.

advanced-filter.js

A filter method that filters out objects in an array that do not match all key/values of an input match object.

CodePen.io: https://codepen.io/talsmonkey/pen/rNOjbVW

advanced-filter-currying.js

A filter method that filters out objects in an array that do not match all key/values of an input match object. The implementation makes used of currying on array functions.

CodePen.io: https://codepen.io/talsmonkey/pen/dyYNrWv

function advancedFilter(collection, match) {
const keysOf = obj => Object.keys(obj)
const toKeyValue = obj => key => ({key, value: obj[key]});
const matchKeyValue = keyValue1 => keyValue2 =>
keyValue1.key === keyValue2.key &&
keyValue1.value === keyValue2.value;
const matchOnAll = keyValues => keyValue => keyValues.some(matchKeyValue(keyValue))
// end functions
const matchKeyValues = keysOf(match).map(toKeyValue(match));
return collection.reduce((acc, cur) => {
const curKeyValues = keysOf(cur).map(toKeyValue(cur));
return matchKeyValues.every(matchOnAll(curKeyValues))
? [...acc, cur]
: acc
}, []);
}
const writePerson = item => document.write(`Name: ${item.name}, LastName: ${item.lastName}<br/>`);
const source = [{name:"Bruce", lastName:"Banner"},
{name:"Bruce", lastName:"Wayne"},
{name: "Clark", lastName: "Kent"}];
document.write("<h2>Source</h2>");
source.forEach(writePerson);
document.write("<br/>");
document.write("<h2>Filter on 'Bruce'</h2>");
advancedFilter(source, {name: "Bruce"}).forEach(writePerson);
document.write("<br/>");
document.write("<h2>Filter on 'Kent'</h2>");
advancedFilter(source, {lastName: "Kent"}).forEach(writePerson);
function advancedFilter(collection, match) {
const keysOf = obj => Object.keys(obj)
const toKeyValue = (obj, key) => ({key, value: obj[key]});
const matchKeyValue = (keyValue1, keyValue2) =>
keyValue1.key === keyValue2.key &&
keyValue1.value === keyValue2.value;
const matchOnAll = (keyValues, keyValue) => keyValues.some(item => matchKeyValue(keyValue, item))
// end functions
const matchKeyValues = keysOf(match).map(item => toKeyValue(match, item));
return collection.reduce((acc, cur) => {
const curKeyValues = keysOf(cur).map(item => toKeyValue(cur, item));
return matchKeyValues.every(item => matchOnAll(curKeyValues, item))
? [...acc, cur]
: acc
}, []);
}
function sum(a) {
function sumAWith(b) {
return a + b;
}
return sumAWith;
}
console.log(sum(3)(5)); //prints 8
function sum(a) {
return b => a + b;
}
console.log(sum(3)(5)); //prints 8
function sum(a, b) {
return a + b;
}
console.log(sum(3, 5)); // prints 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment