Last active
December 15, 2015 05:09
-
-
Save jakesays-old/5207211 to your computer and use it in GitHub Desktop.
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 class StickyEnumerable<T> : IEnumerable<T>, IDisposable | |
{ | |
private IEnumerator<T> innerEnumerator; | |
public StickyEnumerable( IEnumerable<T> items ) | |
{ | |
innerEnumerator = items.GetEnumerator(); | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return innerEnumerator; | |
} | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | |
{ | |
return innerEnumerator; | |
} | |
public void Dispose() | |
{ | |
if (innerEnumerator != null) | |
{ | |
innerEnumerator.Dispose(); | |
} | |
} | |
} | |
IEnumerable<IEnumerable<T>> Chunkify<T>(IEnumerable<T> list, int chunkSize) | |
{ | |
using (var renumerator = new StickyEnumerable(list)) | |
{ | |
do | |
{ | |
var chunk = renumerator.Take(chunkSize).ToArray(); | |
if (chunk.Length == 0) | |
{ | |
yield break; | |
} | |
yield return chunk; | |
} while (true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment