robb / Underscore.m

A DSL for Data Manipulation
https://robb.github.io/Underscore.m/
MIT License
1.47k stars 99 forks source link

Add group_by method for arrays #68

Closed esusatyo closed 8 years ago

esusatyo commented 8 years ago

Say I have this array of dictionaries:

NSArray *names = @[ @{"name" : @"john", "sex" : "male"}, @{"name" : @"alice", "sex" : "female"}, @{"name" : @"mary", "sex" : "female"}];

The command names.group_by(@"sex") will return the array grouped by the sex.

@[ @[@{"name" : @"john", "sex" : "male"}], @[@{"name" : @"alice", "sex" : "female"}, @{"name" : @"mary", "sex" : "female"}]];

I think this may be possible to do right now by chaining, but would be good to have this added to the main methods in Underscore+Functional.h

kvnify commented 8 years ago

I like the idea of supporting the group by API, but I think the API you are proposing doesn't exactly fit with the rest of the API.

Underscore.groupBy(NSArray *source, id key) seems like something that would feel more at home here or the chained vision would be: Underscore.array(names).groupBy(@"sex").unwrap Just my 2 cents.

kvnify commented 8 years ago

I think a problem with this is that you have to assume the elements are NSDictionary objects though. You would have to handle the more general case for this to work.

kvnify commented 8 years ago

Maybe if the groupBy parameter were an NSPredicate.

esusatyo commented 8 years ago

I have just found this commit while trying to make this: https://github.com/robb/Underscore.m/commit/c5c33c60840a2eaa38dbae214bb4bc84a143b398. So @akitsukada tried adding this and we haven't had any version release since then.

I'm testing their implementation right now, and if it's suitable for our need, I might just send a request to do a release from the current development branch.

kvnify commented 8 years ago

Yeah, I'm not sure this project is getting a lot of attention. I've asked the owner if he's interested in letting someone else take it over. But no reply just yet. If I don't get any response I might just start a fork very soon.

robb commented 8 years ago

@khendry I've made you a collaborator, feel free to cut a new release :bow:

kvnify commented 8 years ago

Cool! Thanks.

kvnify commented 8 years ago

Group By will be included in the next release.