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 "groupBy" method #214

Closed
katanacrimson opened this issue Jan 11, 2013 · 11 comments
Closed

Add "groupBy" method #214

katanacrimson opened this issue Jan 11, 2013 · 11 comments

Comments

@katanacrimson
Copy link

Proposed method:

Similar to sortBy, but instead of just sorting by the returned value, it will rebuild the list using the returned value as the entry's index for an array of entries. This would be useful when trying to group or categorize.

e.g.

  { pid: '2384',
    name: 'node run',
    exe: 'node.exe',
    path: 'C:\\Program Files\\nodejs\\node.exe',
    cmdline: 'node.exe',
    memuse: '13348864' },
  { pid: '5000',
    name: 'C:\\Windows\\system32\\wbem\\wmiprvse.exe',
    exe: 'wmiprvse.exe',
    path: 'C:\\Windows\\system32\\wbem\\wmiprvse.exe',
    cmdline: 'WmiPrvSE.exe',
    memuse: '6266880' } ]

with a groupBy method, you'd be able to group individual processes by 'exe' - something that nothing else (that I can see) would allow for.

@durango
Copy link

durango commented Feb 1, 2013

👍 This would affectively eliminate lodash for me :)

@brianmaissy
Copy link
Contributor

If I understand correctly, your proposal would work as follows?

var ungrouped = [
  {name: Alice, age: 12},
  {name: Bob, age: 12},
  {name: Carol, age: 13}
]

function getAge(person, cb){
  cb(null, person.age);
}

async.groupBy(ungrouped, getAge, function(err, results){
  // here, results is {
  //   12: [ {name: Alice, age: 12}, {name: Bob, age: 12} ],
  //   13: [ {name: Carol, age: 13} ]
  // }
});

If so, I would propose implementing it by delegating to async.reduce

@katanacrimson
Copy link
Author

@brianmaissy Spot on - that would be the intention of such a method, yes. I do wonder what the performance costs would be by using reduce for it, though...

@brianmaissy
Copy link
Contributor

You're right, I take that suggestion back. I would implement it similarly to the implementation of sortBy: first by using async.map to calculate the 'grouping criteria' and then manually building the grouped result object with one additional pass through the results.

brianmaissy added a commit to brianmaissy/async that referenced this issue Feb 6, 2013
@caolan
Copy link
Owner

caolan commented Feb 6, 2013

The API and implementation using async.map sound good (since async.reduce is always in series). I do, however, wonder whether this sort of function should be included in async - I imagine it's not particularly common and an async.map followed by some underscore function would do the same thing. Same goes for async.sortBy... how much should the library wander into general array utilities? - discussion on that point welcome

@danmilon
Copy link

danmilon commented Feb 6, 2013

Same feelings. This is not about control flow, which async is for. While it is useful and all, it just doesn't fit here.

@brianmaissy
Copy link
Contributor

Hmm, in retrospect that is a good point. "async.map followed by some underscore function" is basically what its implementation consists of, minus underscore. I figured that if sortBy fits into async's mission, then so would groupBy, even if it is less common. But now that you question sortBy, I think you have a good point, this basically is starting on the process of rebuilding an asynchronous underscore, which is not a great idea.

@brianmaissy
Copy link
Contributor

How did sortBy originally get in? Of course now that it's there, it shouldn't be removed, but maybe we should add something to the documentation saying that this isn't really the goal of async

@caolan
Copy link
Owner

caolan commented Feb 7, 2013

@brianmaissy when creating a new library it sometimes takes a while to find it's appropriate boundaries. I think sortBy shouldn't have been included, and I'd even be quite tempted to deprecate it instead of leaving it hanging around. I don't think there will be many people using it and it's easy to implement yourself. Of course, we'll have to wait for a major-version to introduce API changes like this.

@brianmaissy
Copy link
Contributor

Even if it is not deprecated now, should we put something in the documentation letting contributors know not to use it as a model for other functions?

@dyllan-to-you
Copy link

Discovering this ticket 6 years later, it looks like the feature creep has been officially enshrined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants