4 ms·
The article obfuscates the point. Reducers/transducers are from the FP world. In FP avoiding mutable state is one the main point. dot = field => obj => obj
by agumonkey 7y ago
The article obfuscates the point. Reducers/transducers are from the FP world. In FP avoiding mutable state is one the main point.
dot = field => obj => obj[field] // unsafe
Array.average = self -> reduce(add, self) / self.length // unsafe
slang.filter(s -> s.popularity > 0)
.map(dot('popularity'))
.average()
Bonus points:
- no state
- reusable pure functions
- a tad shorter
- maybe as readable as the `for over state` idiom ? (depending on habits)
- chris_st 7y agoDownside: Still going over the array multiple times.
- agumonkey 7y agofair point, in js it will, in other languages that idiom gets deforested into a single pass though
- sli 7y agoYou could (usually, but not always) compose the functions you're passing into filter and map and use reduce instead to do both operations in a single pass. (Edit: This is a big point in the article -- apologies, I hadn't finished reading it yet.)