Created
June 20, 2019 05:33
-
-
Save joaofnds/9c282648b02785292d908472d09d3fd7 to your computer and use it in GitHub Desktop.
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 customers = [ | |
| { name: 'foo', orders: 4 }, | |
| { name: 'bar', orders: 3 }, | |
| { name: 'baz', orders: 2 }, | |
| { name: 'qux', orders: 1 } | |
| ] | |
| const arrayReducer = (arr, elem) => arr.concat(elem) | |
| const tmap = | |
| transform => | |
| reducer => | |
| (memo, curr) => | |
| reducer(memo, transform(curr)) | |
| const tfilter = | |
| predicate => | |
| reducer => | |
| (memo, curr) => | |
| predicate(curr) ? reducer(memo, curr) : memo | |
| const isGreat = customer => customer.orders > 2 | |
| const getCustomerName = | |
| customer => | |
| isGreat(customer) | |
| ? `${customer.name} the great` | |
| : `${customer.name} the tiny` | |
| const warriorNameTransducer = tmap(getCustomerName) | |
| const upperCaseTransducer = tmap(s => s.toUpperCase()) | |
| const isGreatFilterTransducer = tfilter(isGreat) | |
| const flow = (...fns) => (arg) => fns.reduce((a, f) => f(a), arg) | |
| const flowRight = (...fns) => (arg) => fns.reduceRight((a, f) => f(a), arg) | |
| console.log( | |
| customers.reduce( | |
| flowRight( | |
| isGreatFilterTransducer, | |
| warriorNameTransducer, | |
| upperCaseTransducer, | |
| )(arrayReducer), [] | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment