Last active
April 28, 2022 21:28
-
-
Save dlmanning/47cd75122503214072db to your computer and use it in GitHub Desktop.
tiny little transducer util made with universal-reduce
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
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