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

@@ -13,7 +13,7 @@ namespace LinqRb.UnitTests
{
var range = Enumerable.Range(1, 5);
var rejected = range.Reject(a => a == 3);
foreach(var number in rejected)
foreach (var number in rejected)
{
Assert.NotEqual(number, 3);
}
@@ -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

@@ -13,28 +13,31 @@ namespace System.Linq
/// <returns></returns>
public static IEnumerable<T> Reject<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
foreach(T element in source)
foreach (T element in source)
{
if(!predicate(element)) yield return element;
if (!predicate(element)) yield return element;
}
}
/// <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>
@@ -47,12 +50,12 @@ namespace System.Linq
public static IEnumerable<T> AssocFirstOrDefault<T>(this IEnumerable<IEnumerable<T>> source, T obj)
{
var outer = source.GetEnumerator();
while(outer.MoveNext())
while (outer.MoveNext())
{
var inner = outer.Current.GetEnumerator();
while(inner.MoveNext())
while (inner.MoveNext())
{
if(inner.Current.Equals(obj))
if (inner.Current.Equals(obj))
{
return outer.Current;
}
@@ -70,9 +73,9 @@ namespace System.Linq
public static IEnumerable<T> Compact<T>(this IEnumerable<T> source)
where T : class
{
foreach(var item in source)
foreach (var item in source)
{
if(item != null)
if (item != null)
{
yield return item;
}
@@ -88,7 +91,7 @@ namespace System.Linq
/// <param name="times">Times we should enumerate over the array</param>
public static void Cycle<T>(this IEnumerable<T> source, Action<T> action, int times)
{
for(int i = 0; i < times; i++)
for (int i = 0; i < times; i++)
{
Enumerate(source, action);
}
@@ -102,7 +105,7 @@ namespace System.Linq
/// <param name="action"></param>
public static void Cycle<T>(this IEnumerable<T> source, Action<T> action)
{
while(true)
while (true)
{
Enumerate(source, action);
}
@@ -117,7 +120,7 @@ namespace System.Linq
{
var index = 0;
var enumerator = source.GetEnumerator();
while(enumerator.MoveNext())
while (enumerator.MoveNext())
{
action?.Invoke(enumerator.Current, index);
index++;
@@ -127,10 +130,10 @@ namespace System.Linq
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, Func<T, object> comparer)
{
var items = new HashSet<object>();
foreach(var item in source)
foreach (var item in source)
{
var comparedItem = comparer?.Invoke(item);
if(!items.Contains(comparedItem))
if (!items.Contains(comparedItem))
{
items.Add(comparedItem);
yield return item;
@@ -141,7 +144,7 @@ namespace System.Linq
private static void Enumerate<T>(IEnumerable<T> source, Action<T> action)
{
var enumerator = source.GetEnumerator();
while(enumerator.MoveNext())
while (enumerator.MoveNext())
{
action?.Invoke(enumerator.Current);
}

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" ],