HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Generic method to split provided collection into smaller collections

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
genericmethodintocollectionprovidedsplitsmallercollections

Problem

First time writing a generic method here. Input a List and an int value and output a List> with each member List of the provided int input.

No doubt there are easier / less verbose ways of handling this via LINQ but I wanted to try my hand with a generic method. Please let me know of any ways that I could improve this method.

static List> SplitIntoChunks(List fullBatch, int chunkSize)
{
    if (chunkSize  0)
    {
        numOfChunks++;
    }

    int cellCounter = 0;

    List> splitChunks = new List>();

    for (int chunkNum = 0; chunkNum ();
        for (int index = 0; index < chunkSize; index++)
        {
            if (index < fullBatch.Count)
            {
                chunk.Add(fullBatch[index]);
                cellCounter++;
            }
        }
        splitChunks.Add(chunk);
    }

    return splitChunks;
}

Solution

Your implementation is not bad for a no-LinQ solution. But there's always room for improvement. First I'll provide a LinQ solution that provides a clean way to return a chunked list:

public static List> Split(List collection, int size)
{
    var chunks = new List>();
    var chunkCount = collection.Count() / size;

    if (collection.Count % size > 0)
        chunkCount++;

    for (var i = 0; i < chunkCount; i++)
        chunks.Add(collection.Skip(i * size).Take(size).ToList());

    return chunks;
}


Basically it comes down to this:

  • calculate the count of chunks that are needed



  • loop over the length of chunks



  • use the Enumerable.Skip and Enumerable.Take methods to get chunks



  • return the list of chunks



Now, you implemented a no-LinQ solution so I created one myself too. My implementation doesn't have to calculate the amount of chunks or use two loops to create the list of chunks:

public static List> SplitNoLinq(List collection, int size)
{
    var chunks = new List>();
    var count = 0;
    var temp = new List();

    foreach (var element in collection)
    {
        if (count++ == size)
        {
            chunks.Add(temp);
            temp = new List();
            count = 1;
        }
        temp.Add(element);
    }

    chunks.Add(temp);  
    return chunks;
}


The code iterates over the collection and keeps a counter, adding the iterated item to a temporary list. If the counter equals the desired length of a chunk it will add the temporary list to the return list. At the end, the last chunk is added.

The var keyword:

From the C# Programming Guide:


The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code.

So lines like:

List> splitChunks = new List>();


would become:

var splitChunks = new List>();


Furthermore you could place the code in an extension method, also using an IEnumerable instead of List:

public static class Extensions
{
    public static List> Split(this IEnumerable collection, int size)
    {
        var chunks = new List>();
        var count = 0;
        var temp = new List();

        foreach (var element in collection)
        {
            if (count++ == size)
            {
                chunks.Add(temp);
                temp = new List();
                count = 1;
            }
            temp.Add(element);
        }
        chunks.Add(temp);

        return chunks;
    }
}

//USAGE::
var numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
var chunked = numbers.Split(5);

Code Snippets

public static List<List<T>> Split<T>(List<T> collection, int size)
{
    var chunks = new List<List<T>>();
    var chunkCount = collection.Count() / size;

    if (collection.Count % size > 0)
        chunkCount++;

    for (var i = 0; i < chunkCount; i++)
        chunks.Add(collection.Skip(i * size).Take(size).ToList());

    return chunks;
}
public static List<List<T>> SplitNoLinq<T>(List<T> collection, int size)
{
    var chunks = new List<List<T>>();
    var count = 0;
    var temp = new List<T>();

    foreach (var element in collection)
    {
        if (count++ == size)
        {
            chunks.Add(temp);
            temp = new List<T>();
            count = 1;
        }
        temp.Add(element);
    }

    chunks.Add(temp);  
    return chunks;
}
List<List<T>> splitChunks = new List<List<T>>();
var splitChunks = new List<List<T>>();
public static class Extensions
{
    public static List<List<T>> Split<T>(this IEnumerable<T> collection, int size)
    {
        var chunks = new List<List<T>>();
        var count = 0;
        var temp = new List<T>();

        foreach (var element in collection)
        {
            if (count++ == size)
            {
                chunks.Add(temp);
                temp = new List<T>();
                count = 1;
            }
            temp.Add(element);
        }
        chunks.Add(temp);

        return chunks;
    }
}

//USAGE::
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
var chunked = numbers.Split(5);

Context

StackExchange Code Review Q#90195, answer score: 19

Revisions (0)

No revisions yet.