diff --git a/Readme.md b/Readme.md index 7fd55df..5d8a49a 100644 --- a/Readme.md +++ b/Readme.md @@ -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() { 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())` diff --git a/src/LinqRb.UnitTests/EnumerableTest.cs b/src/LinqRb.UnitTests/EnumerableTest.cs index 46073a9..01f01d2 100644 --- a/src/LinqRb.UnitTests/EnumerableTest.cs +++ b/src/LinqRb.UnitTests/EnumerableTest.cs @@ -6,73 +6,94 @@ using Xunit; namespace LinqRb.UnitTests { - public class EnumerableTest - { - [Fact] - public void TestRejectSuccessful() - { - var range = Enumerable.Range(1, 5); - var rejected = range.Reject(a => a == 3); - foreach(var number in rejected) - { - Assert.NotEqual(number, 3); - } - Assert.True(rejected.Count() == 4); - } + public class EnumerableTest + { + [Fact] + public void TestRejectSuccessful() + { + var range = Enumerable.Range(1, 5); + var rejected = range.Reject(a => a == 3); + foreach (var number in rejected) + { + Assert.NotEqual(number, 3); + } + Assert.True(rejected.Count() == 4); + } - [Fact] - public void TestAssoc() - { - var outer = new List>(); - outer.Add(new List() { "books", "school", "apple", "playtime" }); - outer.Add(new List() { "star", "wars", "vader" }); - var assc = outer.AssocFirstOrDefault("wars"); - Assert.True(assc.Count() == 3); - var assc2 = outer.AssocFirstOrDefault("poop"); - Assert.True(assc2 == null); - } + [Fact] + public void TestAssoc() + { + var outer = new List>(); + outer.Add(new List() { "books", "school", "apple", "playtime" }); + outer.Add(new List() { "star", "wars", "vader" }); + var assc = outer.AssocFirstOrDefault("wars"); + Assert.True(assc.Count() == 3); + var assc2 = outer.AssocFirstOrDefault("poop"); + Assert.True(assc2 == null); + } - [Fact] - public void TestCycle() - { - var p = new List { "a", "b" }; - var count = 0; - p.Cycle(a => count++, 2); - Assert.True(count == 4); - } + [Fact] + public void TestCycle() + { + var p = new List { "a", "b" }; + var count = 0; + p.Cycle(a => count++, 2); + Assert.True(count == 4); + } - [Fact] - public void TestForEach() - { - var counter = 0; - Enumerable.Range(1, 4).ForEach(a => counter++); - Assert.Equal(counter, 4); - } + [Fact] + public void TestForEach() + { + var counter = 0; + Enumerable.Range(1, 4).ForEach(a => counter++); + Assert.Equal(counter, 4); + } - [Fact] - public void TestForEachIndex() - { - var counter = 0; - Enumerable.Range(1, 4).ForEach((a, index) => - { - Assert.Equal(counter, index); - counter++; - }); - Assert.Equal(counter, 4); - } + [Fact] + public void TestForEachIndex() + { + var counter = 0; + Enumerable.Range(1, 4).ForEach((a, index) => + { + Assert.Equal(counter, index); + counter++; + }); + Assert.Equal(counter, 4); + } - [Fact] - public void TestDistinct() - { - var items = new List>(); - items.Add(Tuple.Create("awesom", "1")); - items.Add(Tuple.Create("awesom", "7")); - items.Add(Tuple.Create("awesom", "6")); - items.Add(Tuple.Create("awzomes", "2")); - items.Add(Tuple.Create("pwned", "3")); - items.Add(Tuple.Create("kewlbz", "4")); - var result = items.Distinct(a => a.Item1); - Assert.True(result.Count() == 4); - } - } + [Fact] + public void TestDistinct() + { + var items = new List>(); + items.Add(Tuple.Create("awesom", "1")); + items.Add(Tuple.Create("awesom", "7")); + items.Add(Tuple.Create("awesom", "6")); + items.Add(Tuple.Create("awzomes", "2")); + items.Add(Tuple.Create("pwned", "3")); + items.Add(Tuple.Create("kewlbz", "4")); + var result = items.Distinct(a => a.Item1); + Assert.True(result.Count() == 4); + } + + [Fact] + public void TestChunks() + { + var t = new List(); + 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(); + 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); + } + } } \ No newline at end of file diff --git a/src/LinqRb.UnitTests/project.json b/src/LinqRb.UnitTests/project.json index 0f5bd05..8bcb781 100644 --- a/src/LinqRb.UnitTests/project.json +++ b/src/LinqRb.UnitTests/project.json @@ -15,13 +15,6 @@ }, "frameworks": { "dnxcore50": { - - }, - "dnx50": { - - }, - "dnx451": { - } } } \ No newline at end of file diff --git a/src/LinqRb/Extensions/IEnumerable.cs b/src/LinqRb/Extensions/IEnumerable.cs index 93a03c2..2882e6e 100644 --- a/src/LinqRb/Extensions/IEnumerable.cs +++ b/src/LinqRb/Extensions/IEnumerable.cs @@ -2,149 +2,152 @@ namespace System.Linq { - public static class IEnumerable - { - /// - /// Return Enumerable where action is not true - /// - /// - /// - /// - /// - public static IEnumerable Reject(this IEnumerable source, Func predicate) - { - foreach(T element in source) - { - if(!predicate(element)) yield return element; - } - } - - /// - /// Break a list of items into chunks of a specific size - /// - public static IEnumerable> Chunk(this IEnumerable source, int chunksize) - { - var enumerator = source.GetEnumerator(); - var arr = new List(chunksize); - while (enumerator.MoveNext()) - { - arr.Add(enumerator.Current); - if(arr.Count >= chunksize) - { - yield return arr; - arr = new List(chunksize); - } - } - } - - /// - /// Returns the first array that contains the object - /// - /// - /// - /// - /// - public static IEnumerable AssocFirstOrDefault(this IEnumerable> 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; - } + public static class IEnumerable + { + /// + /// Return Enumerable where action is not true + /// + /// + /// + /// + /// + public static IEnumerable Reject(this IEnumerable source, Func predicate) + { + foreach (T element in source) + { + if (!predicate(element)) yield return element; + } + } - /// - /// Remove all nulls - /// - /// - /// - /// - public static IEnumerable Compact(this IEnumerable source) - where T : class - { - foreach(var item in source) - { - if(item != null) - { - yield return item; - } - } - } + /// + /// Break a list of items into chunks of a specific size + /// + /// + /// How big you wish your chunks to be + public static IEnumerable> Chunk(this IEnumerable source, int chunkSize) + { + var enumerator = source.GetEnumerator(); + var arr = new List(chunkSize); + while (enumerator.MoveNext()) + { + arr.Add(enumerator.Current); + if (arr.Count >= chunkSize) + { + yield return arr; + arr = new List(chunkSize); + } + } + yield return arr; + } - /// - /// Run action over array x times - /// - /// - /// - /// - /// Times we should enumerate over the array - public static void Cycle(this IEnumerable source, Action action, int times) - { - for(int i = 0; i < times; i++) - { - Enumerate(source, action); - } - } + /// + /// Returns the first array that contains the object + /// + /// + /// + /// + /// + public static IEnumerable AssocFirstOrDefault(this IEnumerable> 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; + } - /// - /// Infinate loop over Enumerable calling the action over each one - /// - /// - /// - /// - public static void Cycle(this IEnumerable source, Action action) - { - while(true) - { - Enumerate(source, action); - } - } + /// + /// Remove all nulls + /// + /// + /// + /// + public static IEnumerable Compact(this IEnumerable source) + where T : class + { + foreach (var item in source) + { + if (item != null) + { + yield return item; + } + } + } - public static void ForEach(this IEnumerable source, Action action) - { - Enumerate(source, action); - } + /// + /// Run action over array x times + /// + /// + /// + /// + /// Times we should enumerate over the array + public static void Cycle(this IEnumerable source, Action action, int times) + { + for (int i = 0; i < times; i++) + { + Enumerate(source, action); + } + } - public static void ForEach(this IEnumerable source, Action action) - { - var index = 0; - var enumerator = source.GetEnumerator(); - while(enumerator.MoveNext()) - { - action?.Invoke(enumerator.Current, index); - index++; - } - } + /// + /// Infinate loop over Enumerable calling the action over each one + /// + /// + /// + /// + public static void Cycle(this IEnumerable source, Action action) + { + while (true) + { + Enumerate(source, action); + } + } - public static IEnumerable Distinct(this IEnumerable source, Func comparer) - { - var items = new HashSet(); - foreach(var item in source) - { - var comparedItem = comparer?.Invoke(item); - if(!items.Contains(comparedItem)) - { - items.Add(comparedItem); - yield return item; - } - } - } + public static void ForEach(this IEnumerable source, Action action) + { + Enumerate(source, action); + } - private static void Enumerate(IEnumerable source, Action action) - { - var enumerator = source.GetEnumerator(); - while(enumerator.MoveNext()) - { - action?.Invoke(enumerator.Current); - } - } - } + public static void ForEach(this IEnumerable source, Action action) + { + var index = 0; + var enumerator = source.GetEnumerator(); + while (enumerator.MoveNext()) + { + action?.Invoke(enumerator.Current, index); + index++; + } + } + + public static IEnumerable Distinct(this IEnumerable source, Func comparer) + { + var items = new HashSet(); + foreach (var item in source) + { + var comparedItem = comparer?.Invoke(item); + if (!items.Contains(comparedItem)) + { + items.Add(comparedItem); + yield return item; + } + } + } + + private static void Enumerate(IEnumerable source, Action action) + { + var enumerator = source.GetEnumerator(); + while (enumerator.MoveNext()) + { + action?.Invoke(enumerator.Current); + } + } + } } \ No newline at end of file diff --git a/src/LinqRb/project.json b/src/LinqRb/project.json index af124f9..03b740a 100644 --- a/src/LinqRb/project.json +++ b/src/LinqRb/project.json @@ -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" ], @@ -8,7 +8,7 @@ "frameworks": { "dnx50": { }, - "dnxcore50": { + "dnxcore50": { "dependencies": { "System.Collections": "4.0.11-beta-*", "System.Linq": "4.0.1-beta-*", @@ -26,7 +26,7 @@ }, "net452": { }, - "net46": { + "net46": { } } } \ No newline at end of file