Last active
February 13, 2021 13:12
-
-
Save rexar1988/a08c1dd69471cb4d0b8c3e5c75e22046 to your computer and use it in GitHub Desktop.
JS Function Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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