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

Add _.chunk to Array functions. #1919

Closed
wants to merge 11 commits into from
Closed

Add _.chunk to Array functions. #1919

wants to merge 11 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Nov 5, 2014

Fixes #1891

Petr Archakov added 4 commits October 7, 2014 11:23
Generate a non-negative integers Array by decomposing non-negative
integer number **int** into **count** random integer terms.
_.chunk = function(array, int) {
var result = [];

if (int <= 0) return [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or if array == null (add test)

@megawac
Copy link
Collaborator

megawac commented Nov 5, 2014

That said, 👍 for the method

var result = [];

if (array == null || count <= 0) return [];
if (count >= array.length) return array;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, seems unnecessary, second, we should always produce a new array in case they decide to modify it. I would kill this line

@megawac
Copy link
Collaborator

megawac commented Nov 11, 2014

@keinkonzept mind addressing those comments and squashing the commits

@michaelficarra
Copy link
Collaborator

@megawac: See my comment in #1891 before merging.

@jridgewell
Copy link
Collaborator

@michaelficarra While unfoldr is more generic, I don't think tuples translate very well to JS.

I'm 👍 on this. We use similar functions for batching DB writes constantly.


for (var i = 0, lng = array.length; i < lng;) {
result.push(array.slice(i, i += count));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably write line 655-659 as

var i = 0, length = array && array.length, j = 0;
while (i < length && count >= 0) {
     if (i % count) result[j++] = [];
     result[j].push(array[i]);
}

(untested). I would be curious to see some benchmarks

@megawac megawac mentioned this pull request Jan 19, 2015
16 tasks
@jridgewell
Copy link
Collaborator

Mind removing the trailing spaces on lines 649, 654, 656, and 660?


if (array == null || count <= 0) return [];

for (var i = 0, lng = array.length; i < lng;) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind using length instead of lng?

@stephennancekivell
Copy link

This would be good.

@Florian-R Florian-R mentioned this pull request Mar 31, 2015

if (array == null || count <= 0) return [];

for (var i = 0, length = array.length; i < length;) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind using a while-loop? Every other for-loop in the codebase has an increment expression...

@jridgewell
Copy link
Collaborator

@keinkonzept: Some final nitpicks, and I think this'll be good to merge.

@megawac
Copy link
Collaborator

megawac commented Jul 28, 2015

Based on how often this has been requested, I think it should be added

@michaelficarra
Copy link
Collaborator

I think I agree at this point.

@jridgewell
Copy link
Collaborator

That's three 👍, then. Any last comments on the source before merging?

@michaelficarra
Copy link
Collaborator

I think this implementation is broken for a count of 1. Also, I don't like having a default count. I'll comment more when in front of a computer.

@megawac
Copy link
Collaborator

megawac commented Jul 28, 2015

Seems fine for count 1 (its a tested case for default => 1 and 1). I've squashed the commits down and fixed style issues here:

master...megawac:chunk

@michaelficarra
Copy link
Collaborator

No, definitely broken. It should return an array of singletons.

_.chunk = function(array, count) {
count = count || 1;

if (count <= 1) return array.slice();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct call to slice should instead use slice helper.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, definitely broken. It should return an array of singletons.

Should be count < 1 to fix.

megawac added a commit to megawac/underscore that referenced this pull request Jul 28, 2015
megawac added a commit to megawac/underscore that referenced this pull request Jul 28, 2015
@megawac megawac mentioned this pull request Jul 28, 2015
megawac added a commit to megawac/underscore that referenced this pull request Jul 28, 2015
megawac added a commit to megawac/underscore that referenced this pull request Jul 28, 2015
megawac added a commit to megawac/underscore that referenced this pull request Jul 28, 2015
megawac added a commit to megawac/underscore that referenced this pull request 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 this pull request may close these issues.

_.chunk or _.stride function
5 participants