Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Created June 20, 2019 05:33
Show Gist options
  • Select an option

  • Save joaofnds/9c282648b02785292d908472d09d3fd7 to your computer and use it in GitHub Desktop.

Select an option

Save joaofnds/9c282648b02785292d908472d09d3fd7 to your computer and use it in GitHub Desktop.
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