add chunk

This commit is contained in:
Tommy Parnell
2015-11-01 09:29:58 -05:00
parent 4613ed97c9
commit a18728474b
5 changed files with 235 additions and 212 deletions

View File

@@ -27,6 +27,12 @@ Cycle will run an action for every element in an IEnumerable. This will run fore
this will print `1,2,3,4,1,2,3,4`
## Chunk
Chunk will take the current array and make child arrays, setting the size of the child array to be the specified chunk size
`new List<int>() { 1,2,3,4,5,6}.Chunk(2))` => `[[1,2], [3,4] [5,6]]`
## ForEach
I hope I dont have to explain this one. `ListOfThings.ForEach(a=>a.DoSomething())`

View File

@@ -74,5 +74,26 @@ namespace LinqRb.UnitTests
var result = items.Distinct(a => a.Item1);
Assert.True(result.Count() == 4);
}
[Fact]
public void TestChunks()
{
var t = new List<string>();
var data = Enumerable.Range(1, 20).Select((a) => "ts").Chunk(4).ToList();
Assert.Equal(data.Count, 5);
foreach (var childData in data)
{
Assert.Equal(childData.Count(), 4);
}
}
[Fact]
public void TestOddSizeChunks()
{
var t = new List<string>();
var data = Enumerable.Range(1, 22).Select((a) => "ts").Chunk(4).ToList();
Assert.Equal(data.Count, 6);
Assert.True(data.First(a => a.Count() < 4).Count() == 2);
}
}
}

View File

@@ -15,13 +15,6 @@
},
"frameworks": {
"dnxcore50": {
},
"dnx50": {
},
"dnx451": {
}
}
}

View File

@@ -22,19 +22,22 @@ namespace System.Linq
/// <summary>
/// Break a list of items into chunks of a specific size
/// </summary>
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunksize)
/// <param name="source"></param>
/// <param name="chunkSize">How big you wish your chunks to be</param>
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize)
{
var enumerator = source.GetEnumerator();
var arr = new List<T>(chunksize);
var arr = new List<T>(chunkSize);
while (enumerator.MoveNext())
{
arr.Add(enumerator.Current);
if(arr.Count >= chunksize)
if (arr.Count >= chunkSize)
{
yield return arr;
arr = new List<T>(chunksize);
arr = new List<T>(chunkSize);
}
}
yield return arr;
}
/// <summary>

View File

@@ -1,5 +1,5 @@
{
"version": "1.1.0",
"version": "1.2.0",
"description": "Add additional Linq extensions familiar to ruby ppl",
"authors": [ "Tommy Parnell" ],
"tags": [ "Linq" ],