Created
May 30, 2018 14:41
-
-
Save shx-dev/de1d1a487dd051cfa508927e3ee8c73d to your computer and use it in GitHub Desktop.
Artigos | Array.reduce - O canivete suíço da programação funcional | Compose
This file contains hidden or 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
const fetchUsers = () => { | |
return [ | |
{ name: 'User #1' }, | |
{ name: 'User #2' }, | |
]; | |
} | |
const getUsernames = (users) => { | |
return users.map(user => user.name); | |
} | |
const parseToHtml = (users) => { | |
return users.map(user => `<li>${user}</li>`).join(''); | |
} | |
const compose = (...fns) => { | |
return fns | |
.reverse() | |
.reduce((composition, fn) => { | |
return args => fn(composition(args)); | |
}, args => args); | |
} | |
const listUsers = compose( | |
parseToHtml, getUsernames, fetchUsers, | |
); | |
listUsers(); /* <li>User #1</li><li>User #2</li> */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment