-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
_.groupsOf #696
_.groupsOf #696
Conversation
I've seen this implemented in other languages under the name "chunk". Not that PHP is a bastion of good naming, but for example: |
And “ |
I think it would be better to introduce an > let groupsOf n = unfoldr $ \x -> if null x then Nothing else Just $ splitAt n x
> groupsOf 3 [0..7]
[[0,1,2],[3,4,5],[6,7]]
> LiveScript's Prelude.ls has already implemented it. Take a look at the documentation and the JS source. edit: point-free style |
Add function that takes an array and a chunk size n and produces an array of arrays of size n. Add tests and update index.html to cover new function.
I'm with @michaelficarra on this. |
Thank you for accepting the suggested |
> let sliding n list = if n > length list then [list] else unfoldr (\x -> if n > length x then Nothing else Just (take n x, drop 1 x)) list
> sliding 3 [1..8]
[[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7],[6,7,8]]
> |
@michaelficarra could you translate that to JS for the non-CS readers plz. |
@jdalton: It's Haskell, actually. function groupsOf(n, list) {
return _.unfoldr(function(x){ return x.length === 0 ? null : [x.slice(0, n), x.slice(n)]; }, list);
};
function sliding(n, list){
if(n > list.length) return [list];
return _.unfoldr(function(x){ return n > x.length ? null : [x.slice(0, n), x.slice(1)]; }, list);
}; |
Oh whew thanks, heh that explains why I got errors attempting to convert it from CS to JS via the coffeescript.org demo :P |
It's a shame JavaScript doesn't have the concept of tuples. The caveat to Perhaps adding |
It's cool with me. I just ported it from Haskell - you'll probably be able to make a more efficient version. |
The implementation in this gist is more efficient since it doesn't rely on recursion. |
I'm not such a big fan of adding As for "groupsOf" or "chunk" ... got any good use cases that don't involve starting with a strange data source that passes what should be tuples as a single list? |
@jashkenas don't many client-side paginators depend on logic similar to |
I don't think so ... even if you're showing a list of page numbers, it's often starting from an arbitrary point, and you only need a single "chunk" of numbers, not all of them. For example, when you're on page 7 of google results, you see 2 through 11, with 7 highlighted. Doing an |
Yeah, checking on backbone.paginator, it uses calculated pages, rather than prepared chunks. |
Agreed, they are less readable. The issue with The downsides with |
Sorry for my duplicate #758. For now I use this implementation : https://gist.github.com/3636726. I would be glad to have it in Underscore since I often use that for display purposes. |
I think we should leave this as an |
For those still craving for this functionality, here's the code for
Example:
Oddly enough this was included in the |
As a point of reference, underscore-contrib related function sin underscore.array.builders.js as partition and partitionAll. |
Needed something like this earlier today and was almost sure I would have found it in underscore. Super surprised when I didn't. Take it or leave it.
Includes tests and update to index.html for documentation. Ran
rake doc
, but changes produced seemed too big to be from just me, didn't spend much time looking at the diff though; backed them out, regardless.