Aug 8, 2017 · 1 min read
I feel like this snippet is more difficult to read than it needs to be
const filter = (
fn, arr
) => reduce((acc, curr) => fn(curr) ?
acc.concat([curr]) :
acc, [], arr
);Why write the ternary as a multi-line expression after you’ve omitted the brackets for the callback body?
const filter = (
fn, arr
) => reduce((acc, curr) => {
fn(curr) ?
acc.concat([curr]) :
acc
}, [], arr
);takes up one extra line but makes it clear that the last two expressions are still arguments for the reduce() call
const filter = (
fn, arr
) => reduce((acc, curr) => {
fn(curr) ? acc.concat([curr]) : acc
}, [], arr
);is clearer still and uses the same amount of lines. Great article, I’m getting a lot of out of the series so far!