Created
April 17, 2013 23:08
-
-
Save anderssonjohan/5408521 to your computer and use it in GitHub Desktop.
A pretty basic chunk method I use to chunk, or split, lists of items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IEnumerable<T[]> Chunk<T>( this T[] source, int size ) | |
{ | |
var acc = new List<T>( size ); | |
for ( var i = 0; i < source.Length; i++ ) | |
{ | |
acc.Add( source[i] ); | |
if ( acc.Count < size && i < source.Length - 1 ) | |
continue; | |
yield return acc.ToArray(); | |
acc.Clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment