add chunk
This commit is contained in:
@@ -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`
|
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
|
## ForEach
|
||||||
|
|
||||||
I hope I dont have to explain this one. `ListOfThings.ForEach(a=>a.DoSomething())`
|
I hope I dont have to explain this one. `ListOfThings.ForEach(a=>a.DoSomething())`
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace LinqRb.UnitTests
|
|||||||
{
|
{
|
||||||
var range = Enumerable.Range(1, 5);
|
var range = Enumerable.Range(1, 5);
|
||||||
var rejected = range.Reject(a => a == 3);
|
var rejected = range.Reject(a => a == 3);
|
||||||
foreach(var number in rejected)
|
foreach (var number in rejected)
|
||||||
{
|
{
|
||||||
Assert.NotEqual(number, 3);
|
Assert.NotEqual(number, 3);
|
||||||
}
|
}
|
||||||
@@ -74,5 +74,26 @@ namespace LinqRb.UnitTests
|
|||||||
var result = items.Distinct(a => a.Item1);
|
var result = items.Distinct(a => a.Item1);
|
||||||
Assert.True(result.Count() == 4);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,13 +15,6 @@
|
|||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"dnxcore50": {
|
"dnxcore50": {
|
||||||
|
|
||||||
},
|
|
||||||
"dnx50": {
|
|
||||||
|
|
||||||
},
|
|
||||||
"dnx451": {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,28 +13,31 @@ namespace System.Linq
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IEnumerable<T> Reject<T>(this IEnumerable<T> source, Func<T, bool> predicate)
|
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>
|
/// <summary>
|
||||||
/// Break a list of items into chunks of a specific size
|
/// Break a list of items into chunks of a specific size
|
||||||
/// </summary>
|
/// </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 enumerator = source.GetEnumerator();
|
||||||
var arr = new List<T>(chunksize);
|
var arr = new List<T>(chunkSize);
|
||||||
while (enumerator.MoveNext())
|
while (enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
arr.Add(enumerator.Current);
|
arr.Add(enumerator.Current);
|
||||||
if(arr.Count >= chunksize)
|
if (arr.Count >= chunkSize)
|
||||||
{
|
{
|
||||||
yield return arr;
|
yield return arr;
|
||||||
arr = new List<T>(chunksize);
|
arr = new List<T>(chunkSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
yield return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -47,12 +50,12 @@ namespace System.Linq
|
|||||||
public static IEnumerable<T> AssocFirstOrDefault<T>(this IEnumerable<IEnumerable<T>> source, T obj)
|
public static IEnumerable<T> AssocFirstOrDefault<T>(this IEnumerable<IEnumerable<T>> source, T obj)
|
||||||
{
|
{
|
||||||
var outer = source.GetEnumerator();
|
var outer = source.GetEnumerator();
|
||||||
while(outer.MoveNext())
|
while (outer.MoveNext())
|
||||||
{
|
{
|
||||||
var inner = outer.Current.GetEnumerator();
|
var inner = outer.Current.GetEnumerator();
|
||||||
while(inner.MoveNext())
|
while (inner.MoveNext())
|
||||||
{
|
{
|
||||||
if(inner.Current.Equals(obj))
|
if (inner.Current.Equals(obj))
|
||||||
{
|
{
|
||||||
return outer.Current;
|
return outer.Current;
|
||||||
}
|
}
|
||||||
@@ -70,9 +73,9 @@ namespace System.Linq
|
|||||||
public static IEnumerable<T> Compact<T>(this IEnumerable<T> source)
|
public static IEnumerable<T> Compact<T>(this IEnumerable<T> source)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
foreach(var item in source)
|
foreach (var item in source)
|
||||||
{
|
{
|
||||||
if(item != null)
|
if (item != null)
|
||||||
{
|
{
|
||||||
yield return item;
|
yield return item;
|
||||||
}
|
}
|
||||||
@@ -88,7 +91,7 @@ namespace System.Linq
|
|||||||
/// <param name="times">Times we should enumerate over the array</param>
|
/// <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)
|
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);
|
Enumerate(source, action);
|
||||||
}
|
}
|
||||||
@@ -102,7 +105,7 @@ namespace System.Linq
|
|||||||
/// <param name="action"></param>
|
/// <param name="action"></param>
|
||||||
public static void Cycle<T>(this IEnumerable<T> source, Action<T> action)
|
public static void Cycle<T>(this IEnumerable<T> source, Action<T> action)
|
||||||
{
|
{
|
||||||
while(true)
|
while (true)
|
||||||
{
|
{
|
||||||
Enumerate(source, action);
|
Enumerate(source, action);
|
||||||
}
|
}
|
||||||
@@ -117,7 +120,7 @@ namespace System.Linq
|
|||||||
{
|
{
|
||||||
var index = 0;
|
var index = 0;
|
||||||
var enumerator = source.GetEnumerator();
|
var enumerator = source.GetEnumerator();
|
||||||
while(enumerator.MoveNext())
|
while (enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
action?.Invoke(enumerator.Current, index);
|
action?.Invoke(enumerator.Current, index);
|
||||||
index++;
|
index++;
|
||||||
@@ -127,10 +130,10 @@ namespace System.Linq
|
|||||||
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, Func<T, object> comparer)
|
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, Func<T, object> comparer)
|
||||||
{
|
{
|
||||||
var items = new HashSet<object>();
|
var items = new HashSet<object>();
|
||||||
foreach(var item in source)
|
foreach (var item in source)
|
||||||
{
|
{
|
||||||
var comparedItem = comparer?.Invoke(item);
|
var comparedItem = comparer?.Invoke(item);
|
||||||
if(!items.Contains(comparedItem))
|
if (!items.Contains(comparedItem))
|
||||||
{
|
{
|
||||||
items.Add(comparedItem);
|
items.Add(comparedItem);
|
||||||
yield return item;
|
yield return item;
|
||||||
@@ -141,7 +144,7 @@ namespace System.Linq
|
|||||||
private static void Enumerate<T>(IEnumerable<T> source, Action<T> action)
|
private static void Enumerate<T>(IEnumerable<T> source, Action<T> action)
|
||||||
{
|
{
|
||||||
var enumerator = source.GetEnumerator();
|
var enumerator = source.GetEnumerator();
|
||||||
while(enumerator.MoveNext())
|
while (enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
action?.Invoke(enumerator.Current);
|
action?.Invoke(enumerator.Current);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"description": "Add additional Linq extensions familiar to ruby ppl",
|
"description": "Add additional Linq extensions familiar to ruby ppl",
|
||||||
"authors": [ "Tommy Parnell" ],
|
"authors": [ "Tommy Parnell" ],
|
||||||
"tags": [ "Linq" ],
|
"tags": [ "Linq" ],
|
||||||
|
|||||||
Reference in New Issue
Block a user