Skip to content

Instantly share code, notes, and snippets.

@dlmanning
Last active April 28, 2022 21:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dlmanning/47cd75122503214072db to your computer and use it in GitHub Desktop.
Save dlmanning/47cd75122503214072db to your computer and use it in GitHub Desktop.
tiny little transducer util made with universal-reduce
import reduce from 'universal-reduce'
const transduce = (put, initial) => (collection, ...xfs) =>
reduce(collection, (accum, value, key) =>
put(
accum,
key,
reduce(xfs, (result, xf) => xf(result), value)
),
initial
)
// example
const mapObjectValuesIntoSet = transduce((set, value) => set.add(value), new Set())
mapObectValuesIntoSet({
book1: 'Tale of Two Cities',
book2: 'Anathem',
book3: 'Saved By The Bell: The Inside Story'
}, str => str.toLowerCase())
// returns
// Set { 'tale of two cities', 'anathem', 'saved by the bell: the inside story' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment