by jacobr · 9y ago · 0 comments
pullAllBy(pluck(things, 'bar').map(compose(xor, lol, rofl)).reduce(differenceWith('id'))
Just write your transformations inline and go work on the next feature.If you don't use the builtin transformations, you'll just end up re-implementing them, poorly. And adding to the cognitive overhead with new concepts. And I have to read your code with a fine-toothed comb to ensure it's really side-effect free. I don't advocate turning everything into a named function as in your example though, short one-off functions should all be inline IMO.
Ideally this is how it should be written for maximum readability.
things
|> pluck('bar')
|> map(xor)
|> map(lol)
|> map(rofl)
|> reduce(differenceWith('id')
The code is written equally well without using pipe operator but the proposal to introduce it is in works [1].Here is it using lodash (not even lodash-fp), and this is going to do a single for loop when executing because this is lazy.
_.chain(things)
.pluck('bar')
.map(_.xor)
.map(_.lol)
.map(_.rofl)
.reduce(_.differenceWith('id'))
.value();
It's no less readable than the code you'd write using using unfolded transformations. things
|> pluck('bar')
|> map(compose(rofl, lol, xor))
|> reduce(differenceWith('id')Composing xor, rofl, lol isn't any better (esp in terms of readability) than it is individual maps.
What would be better is this:
const makeHilarious = compose(rofl, lol, xor);
things
|> pluck('bar')
|> map(makeHilarious)
|> reduce(differenceWith('id') things
.map(thing => x.bar)
.map(thing => {
// whatever happens in rofl
// whatever happens in lol
})
.reduce((acc, thing) => {
// more stuff
}, {})
This is code that is easy to understand and safe to change. The maps could be combined into one function body if it's convenient.so someone reading over it can kind of skim down the left side and follow what's happening and scan to the right if they need to understand some part in detail