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` 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())`

View File

@@ -6,73 +6,94 @@ using Xunit;
namespace LinqRb.UnitTests namespace LinqRb.UnitTests
{ {
public class EnumerableTest public class EnumerableTest
{ {
[Fact] [Fact]
public void TestRejectSuccessful() public void TestRejectSuccessful()
{ {
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);
} }
Assert.True(rejected.Count() == 4); Assert.True(rejected.Count() == 4);
} }
[Fact] [Fact]
public void TestAssoc() public void TestAssoc()
{ {
var outer = new List<List<string>>(); var outer = new List<List<string>>();
outer.Add(new List<string>() { "books", "school", "apple", "playtime" }); outer.Add(new List<string>() { "books", "school", "apple", "playtime" });
outer.Add(new List<string>() { "star", "wars", "vader" }); outer.Add(new List<string>() { "star", "wars", "vader" });
var assc = outer.AssocFirstOrDefault("wars"); var assc = outer.AssocFirstOrDefault("wars");
Assert.True(assc.Count() == 3); Assert.True(assc.Count() == 3);
var assc2 = outer.AssocFirstOrDefault("poop"); var assc2 = outer.AssocFirstOrDefault("poop");
Assert.True(assc2 == null); Assert.True(assc2 == null);
} }
[Fact] [Fact]
public void TestCycle() public void TestCycle()
{ {
var p = new List<string> { "a", "b" }; var p = new List<string> { "a", "b" };
var count = 0; var count = 0;
p.Cycle(a => count++, 2); p.Cycle(a => count++, 2);
Assert.True(count == 4); Assert.True(count == 4);
} }
[Fact] [Fact]
public void TestForEach() public void TestForEach()
{ {
var counter = 0; var counter = 0;
Enumerable.Range(1, 4).ForEach(a => counter++); Enumerable.Range(1, 4).ForEach(a => counter++);
Assert.Equal(counter, 4); Assert.Equal(counter, 4);
} }
[Fact] [Fact]
public void TestForEachIndex() public void TestForEachIndex()
{ {
var counter = 0; var counter = 0;
Enumerable.Range(1, 4).ForEach((a, index) => Enumerable.Range(1, 4).ForEach((a, index) =>
{ {
Assert.Equal(counter, index); Assert.Equal(counter, index);
counter++; counter++;
}); });
Assert.Equal(counter, 4); Assert.Equal(counter, 4);
} }
[Fact] [Fact]
public void TestDistinct() public void TestDistinct()
{ {
var items = new List<Tuple<string, string>>(); var items = new List<Tuple<string, string>>();
items.Add(Tuple.Create("awesom", "1")); items.Add(Tuple.Create("awesom", "1"));
items.Add(Tuple.Create("awesom", "7")); items.Add(Tuple.Create("awesom", "7"));
items.Add(Tuple.Create("awesom", "6")); items.Add(Tuple.Create("awesom", "6"));
items.Add(Tuple.Create("awzomes", "2")); items.Add(Tuple.Create("awzomes", "2"));
items.Add(Tuple.Create("pwned", "3")); items.Add(Tuple.Create("pwned", "3"));
items.Add(Tuple.Create("kewlbz", "4")); items.Add(Tuple.Create("kewlbz", "4"));
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);
}
}
} }

View File

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

View File

@@ -2,149 +2,152 @@
namespace System.Linq namespace System.Linq
{ {
public static class IEnumerable public static class IEnumerable
{ {
/// <summary> /// <summary>
/// Return Enumerable where action is not true /// Return Enumerable where action is not true
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="source"></param> /// <param name="source"></param>
/// <param name="predicate"></param> /// <param name="predicate"></param>
/// <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>
/// 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)
{
var enumerator = source.GetEnumerator();
var arr = new List<T>(chunksize);
while (enumerator.MoveNext())
{
arr.Add(enumerator.Current);
if(arr.Count >= chunksize)
{
yield return arr;
arr = new List<T>(chunksize);
}
}
}
/// <summary>
/// Returns the first array that contains the object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static IEnumerable<T> AssocFirstOrDefault<T>(this IEnumerable<IEnumerable<T>> source, T obj)
{
var outer = source.GetEnumerator();
while(outer.MoveNext())
{
var inner = outer.Current.GetEnumerator();
while(inner.MoveNext())
{
if(inner.Current.Equals(obj))
{
return outer.Current;
}
}
}
return null;
}
/// <summary> /// <summary>
/// Remove all nulls /// Break a list of items into chunks of a specific size
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <param name="source"></param>
/// <param name="source"></param> /// <param name="chunkSize">How big you wish your chunks to be</param>
/// <returns></returns> public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize)
public static IEnumerable<T> Compact<T>(this IEnumerable<T> source) {
where T : class var enumerator = source.GetEnumerator();
{ var arr = new List<T>(chunkSize);
foreach(var item in source) while (enumerator.MoveNext())
{ {
if(item != null) arr.Add(enumerator.Current);
{ if (arr.Count >= chunkSize)
yield return item; {
} yield return arr;
} arr = new List<T>(chunkSize);
} }
}
yield return arr;
}
/// <summary> /// <summary>
/// Run action over array x times /// Returns the first array that contains the object
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="source"></param> /// <param name="source"></param>
/// <param name="action"></param> /// <param name="obj"></param>
/// <param name="times">Times we should enumerate over the array</param> /// <returns></returns>
public static void Cycle<T>(this IEnumerable<T> source, Action<T> action, int times) public static IEnumerable<T> AssocFirstOrDefault<T>(this IEnumerable<IEnumerable<T>> source, T obj)
{ {
for(int i = 0; i < times; i++) var outer = source.GetEnumerator();
{ while (outer.MoveNext())
Enumerate(source, action); {
} var inner = outer.Current.GetEnumerator();
} while (inner.MoveNext())
{
if (inner.Current.Equals(obj))
{
return outer.Current;
}
}
}
return null;
}
/// <summary> /// <summary>
/// Infinate loop over Enumerable calling the action over each one /// Remove all nulls
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="source"></param> /// <param name="source"></param>
/// <param name="action"></param> /// <returns></returns>
public static void Cycle<T>(this IEnumerable<T> source, Action<T> action) public static IEnumerable<T> Compact<T>(this IEnumerable<T> source)
{ where T : class
while(true) {
{ foreach (var item in source)
Enumerate(source, action); {
} if (item != null)
} {
yield return item;
}
}
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) /// <summary>
{ /// Run action over array x times
Enumerate(source, action); /// </summary>
} /// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="action"></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)
{
for (int i = 0; i < times; i++)
{
Enumerate(source, action);
}
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action) /// <summary>
{ /// Infinate loop over Enumerable calling the action over each one
var index = 0; /// </summary>
var enumerator = source.GetEnumerator(); /// <typeparam name="T"></typeparam>
while(enumerator.MoveNext()) /// <param name="source"></param>
{ /// <param name="action"></param>
action?.Invoke(enumerator.Current, index); public static void Cycle<T>(this IEnumerable<T> source, Action<T> action)
index++; {
} while (true)
} {
Enumerate(source, action);
}
}
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, Func<T, object> comparer) public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{ {
var items = new HashSet<object>(); Enumerate(source, action);
foreach(var item in source) }
{
var comparedItem = comparer?.Invoke(item);
if(!items.Contains(comparedItem))
{
items.Add(comparedItem);
yield return item;
}
}
}
private static void Enumerate<T>(IEnumerable<T> source, Action<T> action) public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
{ {
var enumerator = source.GetEnumerator(); var index = 0;
while(enumerator.MoveNext()) var enumerator = source.GetEnumerator();
{ while (enumerator.MoveNext())
action?.Invoke(enumerator.Current); {
} action?.Invoke(enumerator.Current, index);
} index++;
} }
}
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, Func<T, object> comparer)
{
var items = new HashSet<object>();
foreach (var item in source)
{
var comparedItem = comparer?.Invoke(item);
if (!items.Contains(comparedItem))
{
items.Add(comparedItem);
yield return item;
}
}
}
private static void Enumerate<T>(IEnumerable<T> source, Action<T> action)
{
var enumerator = source.GetEnumerator();
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", "description": "Add additional Linq extensions familiar to ruby ppl",
"authors": [ "Tommy Parnell" ], "authors": [ "Tommy Parnell" ],
"tags": [ "Linq" ], "tags": [ "Linq" ],
@@ -8,7 +8,7 @@
"frameworks": { "frameworks": {
"dnx50": { "dnx50": {
}, },
"dnxcore50": { "dnxcore50": {
"dependencies": { "dependencies": {
"System.Collections": "4.0.11-beta-*", "System.Collections": "4.0.11-beta-*",
"System.Linq": "4.0.1-beta-*", "System.Linq": "4.0.1-beta-*",
@@ -26,7 +26,7 @@
}, },
"net452": { "net452": {
}, },
"net46": { "net46": {
} }
} }
} }