DEV Community

Discussion on: Array Chunking

Collapse
 
yever profile image
Ya'ar Hever

If you're already in the mood for elegance, why not use a single expression arrow function?

const chunk = (xs, n) => (xs.length <= n) ? [xs] : [
  xs.slice(0, n),
  ...chunk(xs.slice(n), n)
];
Collapse
 
ycmjason profile image
YCM Jason

because IMO that's not more elegant.

I think guard clauses should live in the if statement that has nothing to do with the rest of the code. I think it is easier to read.

But I believe there is no absolute "elegance". Programming is just like art, different people perceive a piece differently.

Thread Thread
 
moopet profile image
Ben Sinclair

Mm, Jason's version looks more readable to me. It's laid out a little more like you would if you were matching patterns in Haskell or something.