OCTotallyLazy
OCTotallyLazy copied to clipboard
NSArray groupBy can't convert to NSDictionary or map keys
First, it would be nice to see a version which returns an NSDictionary
, which is its behavior in Clojure or Ruby (though I see this matches the behavior of the original TL). But chaining groupBy
with asDictionary
doesn't run successfully, as I'd expect.
But even as an array of tuples, seemingly simple things like mapping over keys seems broken. It seems to flatten the underlying array via the asSequence
call, which means I can't derive the list of all keys returned from the grouping.
Am I missing something? Is there a better way? I am not deeply familiar with the original Java library; does this port imply better knowledge of that than I have? Documentation is sparse.
NSArray *groupedUsers = [users groupBy:^id(User *user) {
return user.firstLetterOfName;
];
[groupedUsers asDictionary]; // Kerpow! I don't know what to do with groups.
NSArray *keys = [groupedUsers map:^id(Group *group) {
return group.key; // Kerpow! I am not a group.
}];
This is especially incongruous because foreach
appears to do the right thing.
Thanks for pointing this out Brian... I'll have a look into it. I suspect I've done something dumb. A group is just a keyed sequence. The final example you laid out should work as expected. That's more or less what the test case does.
I'll improve the test case, and look to add an implementation of groupBy that returns the NSDictionary as you suggest.