Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_.chunk or _.stride function #1891

Closed
Soviut opened this issue Oct 15, 2014 · 8 comments
Closed

_.chunk or _.stride function #1891

Soviut opened this issue Oct 15, 2014 · 8 comments

Comments

@Soviut
Copy link

Soviut commented Oct 15, 2014

A common function I'm sure many of us run into (especially regarding pagination) is the need to break an array into chunks of a specified length. I propose a _.chunk() (or _.stride if "chunk" isn't clear enough). It would also be useful in a number of other underscore features, including my proposal for _.objects #1890.

The first value would be the list, the second value would be the chunk-size/stride-length. If the chunk size doesn't divide evenly into the length of the list, the final item should be the remainder.

_.chunk([1,2,3,4,5,6,7,8], 3);

Would yield

[[1,2,3], [4,5,6], [7,8]]

It might be worth having an optional "remainder" boolean argument that could omit the remainder if set to false, but should probably be true by default. This would be useful in cases where the chunks were going to be mapped using _.object and dangling arguments weren't desirable.

_.chunk([1,2,3,4,5,6,7,8], 3, false);

Would yield

[[1,2,3], [4,5,6]]
@jdalton
Copy link
Contributor

jdalton commented Oct 15, 2014

In my implementation I opt'ed for not supporting a "remainder" function as Underscore and the like have sided on not destroying data when possible.

@Soviut
Copy link
Author

Soviut commented Oct 15, 2014

Seems reasonable. I can't actually think of any situation where this would outright break anything.

@Soviut
Copy link
Author

Soviut commented Oct 15, 2014

@jdalton Could you post a link to the function in the edge source?

@joshuacc
Copy link

FYI, we have _.chunk in contrib as well, though the implementation is slightly different. Note how padding behaves in contrib versus this proposal. Contrib's _.chunkAll is a closer match to this proposal.

@Soviut
Copy link
Author

Soviut commented Oct 15, 2014

@joshuacc I feel like their implementation for _.chunk is inelegant, especially since they also have a _.chunkAll function. The implementation they give drops remainders by default which seems counter to the "don't destroy data" ideal that @jdalton mentioned. Futher, the need to supply "ignorable" items to the list to satisfy the length requirements feels very fragile.

Since both implementations are in contrib the need for a "remainder" option seems useful, however, it shouldn't truncate the remainder by default, in my opinion. Having it as an optional boolean also means it would require less code to switch between modes in conditional situations; Requiring only an expression versus being wrapped in an if statement.

@jdalton
Copy link
Contributor

jdalton commented Oct 15, 2014

@jdalton Could you post a link to the function in the edge source?

See my comment on issue #1890.

Not dropping remainders by default also allows adding an option in at a later time if the request is popular enough.

@joshuacc
Copy link

@Soviut Yeah. I don't have any objections to keeping remainders by default. Just wanted to make sure folks in this thread were aware of contrib's current implementation when discussing the proposal.

If this makes it into Underscore, then we'll revise contrib to play nicely with it. 😄

@michaelficarra
Copy link
Collaborator

See #696. I don't think this will be accepted, and if it was I'd much rather see unfoldr instead.

This was referenced Jul 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants