Skip to content

Instantly share code, notes, and snippets.

@rexar1988
Last active February 13, 2021 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexar1988/a08c1dd69471cb4d0b8c3e5c75e22046 to your computer and use it in GitHub Desktop.
Save rexar1988/a08c1dd69471cb4d0b8c3e5c75e22046 to your computer and use it in GitHub Desktop.
JS Function Compose
function compose(...functions) {
return argument => functions.reduceRight((acc, current) => current(acc), argument);
}
function pipe(...functions) {
return argument => functions.reduce((acc, current) => current(acc), argument);
}
const users = [
{
username: 'John Doe 1',
age: 5,
},
{
username: 'John Doe 2',
age: 10,
},
{
username: 'John Doe 3',
age: 20,
},
{
username: 'John Doe 4',
age: 30,
},
{
username: 'John Doe 5',
age: 40,
},
{
username: 'John Doe 6',
age: 50,
},
];
const result = compose(
(users) => users.filter(user => user.age > 11),
(users) => users.map(user => {
user.age = user.age + 1;
return user;
}),
);
console.log('result', result(users));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment