From 36a4b8808b8dd7e6bbd3e69741041d90471c184d Mon Sep 17 00:00:00 2001 From: VarnaSuresh Date: Thu, 4 Oct 2018 18:16:24 +0530 Subject: [PATCH 1/5] ToLookup method and tests --- ShittyLINQ/ToLookup.cs | 105 +++++++++++++++++++++++++++++++ ShittyLinqTests/ToLookupTests.cs | 42 +++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 ShittyLINQ/ToLookup.cs create mode 100644 ShittyLinqTests/ToLookupTests.cs diff --git a/ShittyLINQ/ToLookup.cs b/ShittyLINQ/ToLookup.cs new file mode 100644 index 0000000..3d14978 --- /dev/null +++ b/ShittyLINQ/ToLookup.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + /// + /// Creates a lookup from an IEnumerable + /// + /// Set of elements to be converted to lookup + /// Key selector to determine the key of the lookup + /// Source type + /// Key type + /// A lookup made from source + /// If source, keySelector or element Selector is null + public static Dictionary> ToLookup( + this IEnumerable source, + Func keySelector) + { + return ToLookup(source, keySelector, t => t, EqualityComparer.Default); + } + + /// + /// Creates a lookup from an IEnumerable + /// + /// Set of elements to be converted to lookup + /// Key selector to determine the key of the lookup + /// Element selector to determine the value of the lookup + /// Source type + /// Key type + /// Value type + /// A lookup made from source + /// If source, keySelector or element Selector is null + public static Dictionary> ToLookup( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return source.ToLookup(keySelector, elementSelector, EqualityComparer.Default); + } + + /// + /// Creates a lookup from an IEnumerable + /// + /// Set of elements to be converted to lookup + /// Key selector to determine the key of the lookup + /// Comparer to be used to determine equality between elements + /// Source type + /// Key type + /// A lookup made from source + /// If source, keySelector or element Selector is null + public static Dictionary> ToLookup( + this IEnumerable source, + Func keySelector, + IEqualityComparer comparer) + { + return ToLookup(source, keySelector, t => t, comparer); + } + + /// + /// Creates a lookup from an IEnumerable + /// + /// Set of elements to be converted to lookup + /// Key selector to determine the key of the lookup + /// Element selector to determine the value of the lookup + /// Comparer to be used to determine equality between elements + /// Source type + /// Key type + /// Value type + /// A lookup made from source + /// If source, keySelector or element Selector is null + public static Dictionary> ToLookup( + this IEnumerable source, + Func keySelector, + Func elementSelector, + IEqualityComparer comparer) + { + if (source == null || keySelector == null || elementSelector == null) throw new ArgumentNullException(); + + List tempList; + IEnumerable existingValues; + + Dictionary> ret = new Dictionary>(comparer ?? EqualityComparer.Default); + foreach (TSource item in source) + { + existingValues = ret.GetValueOrDefault(keySelector(item)); + if (existingValues == null) + { + tempList = new List(); + } + else + { + tempList = existingValues.ToList(); + ret.Remove(keySelector(item)); + } + tempList.Add(elementSelector(item)); + ret.Add(keySelector(item), tempList); + + } + return ret; + } + } + +} \ No newline at end of file diff --git a/ShittyLinqTests/ToLookupTests.cs b/ShittyLinqTests/ToLookupTests.cs new file mode 100644 index 0000000..cc38edb --- /dev/null +++ b/ShittyLinqTests/ToLookupTests.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using ShittyTests.TestHelpers; + +namespace ShittyTests +{ + [TestClass] + public class ToLookupTests + { + [TestMethod] + public void ToLookup_Numbers() + { + IEnumerable numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + var result = numbers.ToLookup(x => x, x => true); + + Assert.AreEqual(result.Count, 9); + + foreach (int number in numbers) + { + Assert.IsTrue(result.ContainsKey(number)); + } + } + + [TestMethod] + public void ToLookup_Person() + { + var adam = new Person("Adam", 20, "Arquitech", "Amber"); + var brian = new Person("Brian", 45, "Arquitech", "Blue"); + var charles = new Person("Charles", 33, "Arquitech", "Cyan"); + var dani = new Person("Dani", 33, "Developer", "Deep Purple"); + + IEnumerable people = new[] { adam, brian, charles, dani }; + + var result = people.ToLookup(person => person.Age); + + Assert.AreEqual(result.TryGetValue(brian.Age, out var expectedBrian), true); + Assert.IsNotNull(expectedBrian); + } + } +} \ No newline at end of file From bab461eb5c6a0a8c398875af9d5f21d70ad3b61d Mon Sep 17 00:00:00 2001 From: Varna Suresh Date: Sun, 7 Oct 2018 00:01:58 +0530 Subject: [PATCH 2/5] GroupBy method and tests --- ShittyLINQ/GroupBy.cs | 180 ++++++++++++++++++++++++++++++++ ShittyLinqTests/GroupByTests.cs | 47 +++++++++ 2 files changed, 227 insertions(+) create mode 100644 ShittyLINQ/GroupBy.cs create mode 100644 ShittyLinqTests/GroupByTests.cs diff --git a/ShittyLINQ/GroupBy.cs b/ShittyLINQ/GroupBy.cs new file mode 100644 index 0000000..0ab4165 --- /dev/null +++ b/ShittyLINQ/GroupBy.cs @@ -0,0 +1,180 @@ +using System; +using System.Collections.Generic; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// Source type + /// Key type + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector) + { + return GroupBy(source, keySelector, t => t, EqualityComparer.Default); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector, + IEqualityComparer comparer) + { + return GroupBy(source, keySelector, t => t, comparer); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to map each source element to an element in an IGrouping + /// Source type + /// Key type + /// Value type + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return GroupBy(source, keySelector, elementSelector, EqualityComparer.Default); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to map each source element to an element in an IGrouping + /// Source type + /// Key type + /// Value type + /// An IEqualityComparer to compare keys with. + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector, + IEqualityComparer comparer) + { + if (source == null || keySelector == null || elementSelector == null) + throw new ArgumentNullException(); + + Dictionary> res = new Dictionary>(comparer ?? EqualityComparer.Default); + + foreach (TSource item in source) + { + var subset = source.Where(b => keySelector(b).Equals(keySelector(item))).Select(a => elementSelector(a)).ToList(); + if (!res.ContainsKey(keySelector(item))) + { + res.Add(keySelector(item), subset); + } + } + return res; + } + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to create a result value from each group. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// The type of the result value returned by resultSelector + /// A collection of elements of type TResult where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector) + { + return GroupBy(source, keySelector, t => t, resultSelector, EqualityComparer.Default); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to create a result value from each group. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// The type of the result value returned by resultSelector + /// A collection of elements of type TResult where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + return GroupBy(source, keySelector, t => t, resultSelector, comparer); + } + + + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to map each source element to an element in an IGrouping + /// A function to create a result value from each group. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// Value type + /// The type of the result value returned by resultSelector + /// A collection of elements of type TResult where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static IEnumerable GroupBy( + IEnumerable source, + Func keySelector, + Func elementSelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + if (source == null || keySelector == null || elementSelector == null || resultSelector == null || comparer == null) + throw new ArgumentNullException(); + + List res = new List(); + + foreach (TSource item in source) + { + var subset = source.Where(b => keySelector(b).Equals(keySelector(item))).Select(a => elementSelector(a)).ToList(); + if (!res.Contains(resultSelector(keySelector(item), subset))) + { + res.Add(resultSelector(keySelector(item), subset)); + } + } + return res; + } + } +} diff --git a/ShittyLinqTests/GroupByTests.cs b/ShittyLinqTests/GroupByTests.cs new file mode 100644 index 0000000..2eef660 --- /dev/null +++ b/ShittyLinqTests/GroupByTests.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using ShittyTests.TestHelpers; + +namespace ShittyTests +{ + [TestClass] + public class GroupByTests + { + + [TestMethod] + public void GroupBy_Person() + { + var adam = new Person("Adam", 20, "Arquitech", "Amber"); + var brian = new Person("Brian", 45, "Arquitech", "Blue"); + var charles = new Person("Charles", 33, "Arquitech", "Cyan"); + var dani = new Person("Dani", 33, "Developer", "Deep Purple"); + IEnumerable people = new[] { adam, brian, charles, dani }; + + var result = people.GroupBy(person => person.Age); + + Assert.AreEqual(result.Count, 3); + Assert.AreEqual(result.GetValueOrDefault(20).First(), adam); + } + + [TestMethod] + public void GroupBy_Person_WithResultSelector() + { + var adam = new Person("Adam", 20, "Arquitech", "Amber"); + var brian = new Person("Brian", 45, "Arquitech", "Blue"); + var charles = new Person("Charles", 33, "Arquitech", "Cyan"); + var dani = new Person("Dani", 33, "Developer", "Deep Purple"); + IEnumerable people = new[] { adam, brian, charles, dani }; + + var result = people.GroupBy(person => person.Age, + (baseAge, ages) => new + { + Key = baseAge, + Count = (int)ages.Count() + }); + + Assert.AreEqual(result.ToList().Count, 3); + Assert.AreEqual(result.ToList().Max(a => a.Count), 2); + } + } +} \ No newline at end of file From b5a4d8bf4eff1168c74f83c66e2677b65a2eebf0 Mon Sep 17 00:00:00 2001 From: Varna Suresh Date: Sun, 7 Oct 2018 00:16:49 +0530 Subject: [PATCH 3/5] Revert "GroupBy method and tests" This reverts commit bab461eb5c6a0a8c398875af9d5f21d70ad3b61d. --- ShittyLINQ/GroupBy.cs | 180 -------------------------------- ShittyLinqTests/GroupByTests.cs | 47 --------- 2 files changed, 227 deletions(-) delete mode 100644 ShittyLINQ/GroupBy.cs delete mode 100644 ShittyLinqTests/GroupByTests.cs diff --git a/ShittyLINQ/GroupBy.cs b/ShittyLINQ/GroupBy.cs deleted file mode 100644 index 0ab4165..0000000 --- a/ShittyLINQ/GroupBy.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ShittyLINQ -{ - public static partial class Extensions - { - /// - /// Groups the elements of a sequence. - /// - /// An IEnumerable whose elements to group. - /// A function to extract the key for each element. - /// Source type - /// Key type - /// A collection of elements where each element represents a projection over a group and its key. - /// If source, keySelector or element Selector is null - public static Dictionary> GroupBy( - this IEnumerable source, - Func keySelector) - { - return GroupBy(source, keySelector, t => t, EqualityComparer.Default); - } - - - /// - /// Groups the elements of a sequence. - /// - /// An IEnumerable whose elements to group. - /// A function to extract the key for each element. - /// An IEqualityComparer to compare keys with. - /// Source type - /// Key type - /// A collection of elements where each element represents a projection over a group and its key. - /// If source, keySelector or element Selector is null - public static Dictionary> GroupBy( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return GroupBy(source, keySelector, t => t, comparer); - } - - - /// - /// Groups the elements of a sequence. - /// - /// An IEnumerable whose elements to group. - /// A function to extract the key for each element. - /// A function to map each source element to an element in an IGrouping - /// Source type - /// Key type - /// Value type - /// A collection of elements where each element represents a projection over a group and its key. - /// If source, keySelector or element Selector is null - public static Dictionary> GroupBy( - this IEnumerable source, - Func keySelector, - Func elementSelector) - { - return GroupBy(source, keySelector, elementSelector, EqualityComparer.Default); - } - - - /// - /// Groups the elements of a sequence. - /// - /// An IEnumerable whose elements to group. - /// A function to extract the key for each element. - /// A function to map each source element to an element in an IGrouping - /// Source type - /// Key type - /// Value type - /// An IEqualityComparer to compare keys with. - /// A collection of elements where each element represents a projection over a group and its key. - /// If source, keySelector or element Selector is null - public static Dictionary> GroupBy( - this IEnumerable source, - Func keySelector, - Func elementSelector, - IEqualityComparer comparer) - { - if (source == null || keySelector == null || elementSelector == null) - throw new ArgumentNullException(); - - Dictionary> res = new Dictionary>(comparer ?? EqualityComparer.Default); - - foreach (TSource item in source) - { - var subset = source.Where(b => keySelector(b).Equals(keySelector(item))).Select(a => elementSelector(a)).ToList(); - if (!res.ContainsKey(keySelector(item))) - { - res.Add(keySelector(item), subset); - } - } - return res; - } - - /// - /// Groups the elements of a sequence. - /// - /// An IEnumerable whose elements to group. - /// A function to extract the key for each element. - /// A function to create a result value from each group. - /// An IEqualityComparer to compare keys with. - /// Source type - /// Key type - /// The type of the result value returned by resultSelector - /// A collection of elements of type TResult where each element represents a projection over a group and its key. - /// If source, keySelector or element Selector is null - public static IEnumerable GroupBy( - this IEnumerable source, - Func keySelector, - Func, TResult> resultSelector) - { - return GroupBy(source, keySelector, t => t, resultSelector, EqualityComparer.Default); - } - - - /// - /// Groups the elements of a sequence. - /// - /// An IEnumerable whose elements to group. - /// A function to extract the key for each element. - /// A function to create a result value from each group. - /// An IEqualityComparer to compare keys with. - /// Source type - /// Key type - /// The type of the result value returned by resultSelector - /// A collection of elements of type TResult where each element represents a projection over a group and its key. - /// If source, keySelector or element Selector is null - public static IEnumerable GroupBy( - this IEnumerable source, - Func keySelector, - Func, TResult> resultSelector, - IEqualityComparer comparer) - { - return GroupBy(source, keySelector, t => t, resultSelector, comparer); - } - - - - - /// - /// Groups the elements of a sequence. - /// - /// An IEnumerable whose elements to group. - /// A function to extract the key for each element. - /// A function to map each source element to an element in an IGrouping - /// A function to create a result value from each group. - /// An IEqualityComparer to compare keys with. - /// Source type - /// Key type - /// Value type - /// The type of the result value returned by resultSelector - /// A collection of elements of type TResult where each element represents a projection over a group and its key. - /// If source, keySelector or element Selector is null - public static IEnumerable GroupBy( - IEnumerable source, - Func keySelector, - Func elementSelector, - Func, TResult> resultSelector, - IEqualityComparer comparer) - { - if (source == null || keySelector == null || elementSelector == null || resultSelector == null || comparer == null) - throw new ArgumentNullException(); - - List res = new List(); - - foreach (TSource item in source) - { - var subset = source.Where(b => keySelector(b).Equals(keySelector(item))).Select(a => elementSelector(a)).ToList(); - if (!res.Contains(resultSelector(keySelector(item), subset))) - { - res.Add(resultSelector(keySelector(item), subset)); - } - } - return res; - } - } -} diff --git a/ShittyLinqTests/GroupByTests.cs b/ShittyLinqTests/GroupByTests.cs deleted file mode 100644 index 2eef660..0000000 --- a/ShittyLinqTests/GroupByTests.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using ShittyLINQ; -using ShittyTests.TestHelpers; - -namespace ShittyTests -{ - [TestClass] - public class GroupByTests - { - - [TestMethod] - public void GroupBy_Person() - { - var adam = new Person("Adam", 20, "Arquitech", "Amber"); - var brian = new Person("Brian", 45, "Arquitech", "Blue"); - var charles = new Person("Charles", 33, "Arquitech", "Cyan"); - var dani = new Person("Dani", 33, "Developer", "Deep Purple"); - IEnumerable people = new[] { adam, brian, charles, dani }; - - var result = people.GroupBy(person => person.Age); - - Assert.AreEqual(result.Count, 3); - Assert.AreEqual(result.GetValueOrDefault(20).First(), adam); - } - - [TestMethod] - public void GroupBy_Person_WithResultSelector() - { - var adam = new Person("Adam", 20, "Arquitech", "Amber"); - var brian = new Person("Brian", 45, "Arquitech", "Blue"); - var charles = new Person("Charles", 33, "Arquitech", "Cyan"); - var dani = new Person("Dani", 33, "Developer", "Deep Purple"); - IEnumerable people = new[] { adam, brian, charles, dani }; - - var result = people.GroupBy(person => person.Age, - (baseAge, ages) => new - { - Key = baseAge, - Count = (int)ages.Count() - }); - - Assert.AreEqual(result.ToList().Count, 3); - Assert.AreEqual(result.ToList().Max(a => a.Count), 2); - } - } -} \ No newline at end of file From bc3510f80ecf2496e6abf2e0e827ea1703472c5f Mon Sep 17 00:00:00 2001 From: Varna Suresh Date: Sun, 7 Oct 2018 00:26:52 +0530 Subject: [PATCH 4/5] GroupBy method and tests --- ShittyLINQ/GroupBy.cs | 180 ++++++++++++++++++++++++++++++++ ShittyLinqTests/GroupByTests.cs | 47 +++++++++ 2 files changed, 227 insertions(+) create mode 100644 ShittyLINQ/GroupBy.cs create mode 100644 ShittyLinqTests/GroupByTests.cs diff --git a/ShittyLINQ/GroupBy.cs b/ShittyLINQ/GroupBy.cs new file mode 100644 index 0000000..0ab4165 --- /dev/null +++ b/ShittyLINQ/GroupBy.cs @@ -0,0 +1,180 @@ +using System; +using System.Collections.Generic; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// Source type + /// Key type + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector) + { + return GroupBy(source, keySelector, t => t, EqualityComparer.Default); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector, + IEqualityComparer comparer) + { + return GroupBy(source, keySelector, t => t, comparer); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to map each source element to an element in an IGrouping + /// Source type + /// Key type + /// Value type + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector) + { + return GroupBy(source, keySelector, elementSelector, EqualityComparer.Default); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to map each source element to an element in an IGrouping + /// Source type + /// Key type + /// Value type + /// An IEqualityComparer to compare keys with. + /// A collection of elements where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static Dictionary> GroupBy( + this IEnumerable source, + Func keySelector, + Func elementSelector, + IEqualityComparer comparer) + { + if (source == null || keySelector == null || elementSelector == null) + throw new ArgumentNullException(); + + Dictionary> res = new Dictionary>(comparer ?? EqualityComparer.Default); + + foreach (TSource item in source) + { + var subset = source.Where(b => keySelector(b).Equals(keySelector(item))).Select(a => elementSelector(a)).ToList(); + if (!res.ContainsKey(keySelector(item))) + { + res.Add(keySelector(item), subset); + } + } + return res; + } + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to create a result value from each group. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// The type of the result value returned by resultSelector + /// A collection of elements of type TResult where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector) + { + return GroupBy(source, keySelector, t => t, resultSelector, EqualityComparer.Default); + } + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to create a result value from each group. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// The type of the result value returned by resultSelector + /// A collection of elements of type TResult where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static IEnumerable GroupBy( + this IEnumerable source, + Func keySelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + return GroupBy(source, keySelector, t => t, resultSelector, comparer); + } + + + + + /// + /// Groups the elements of a sequence. + /// + /// An IEnumerable whose elements to group. + /// A function to extract the key for each element. + /// A function to map each source element to an element in an IGrouping + /// A function to create a result value from each group. + /// An IEqualityComparer to compare keys with. + /// Source type + /// Key type + /// Value type + /// The type of the result value returned by resultSelector + /// A collection of elements of type TResult where each element represents a projection over a group and its key. + /// If source, keySelector or element Selector is null + public static IEnumerable GroupBy( + IEnumerable source, + Func keySelector, + Func elementSelector, + Func, TResult> resultSelector, + IEqualityComparer comparer) + { + if (source == null || keySelector == null || elementSelector == null || resultSelector == null || comparer == null) + throw new ArgumentNullException(); + + List res = new List(); + + foreach (TSource item in source) + { + var subset = source.Where(b => keySelector(b).Equals(keySelector(item))).Select(a => elementSelector(a)).ToList(); + if (!res.Contains(resultSelector(keySelector(item), subset))) + { + res.Add(resultSelector(keySelector(item), subset)); + } + } + return res; + } + } +} diff --git a/ShittyLinqTests/GroupByTests.cs b/ShittyLinqTests/GroupByTests.cs new file mode 100644 index 0000000..2eef660 --- /dev/null +++ b/ShittyLinqTests/GroupByTests.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using ShittyTests.TestHelpers; + +namespace ShittyTests +{ + [TestClass] + public class GroupByTests + { + + [TestMethod] + public void GroupBy_Person() + { + var adam = new Person("Adam", 20, "Arquitech", "Amber"); + var brian = new Person("Brian", 45, "Arquitech", "Blue"); + var charles = new Person("Charles", 33, "Arquitech", "Cyan"); + var dani = new Person("Dani", 33, "Developer", "Deep Purple"); + IEnumerable people = new[] { adam, brian, charles, dani }; + + var result = people.GroupBy(person => person.Age); + + Assert.AreEqual(result.Count, 3); + Assert.AreEqual(result.GetValueOrDefault(20).First(), adam); + } + + [TestMethod] + public void GroupBy_Person_WithResultSelector() + { + var adam = new Person("Adam", 20, "Arquitech", "Amber"); + var brian = new Person("Brian", 45, "Arquitech", "Blue"); + var charles = new Person("Charles", 33, "Arquitech", "Cyan"); + var dani = new Person("Dani", 33, "Developer", "Deep Purple"); + IEnumerable people = new[] { adam, brian, charles, dani }; + + var result = people.GroupBy(person => person.Age, + (baseAge, ages) => new + { + Key = baseAge, + Count = (int)ages.Count() + }); + + Assert.AreEqual(result.ToList().Count, 3); + Assert.AreEqual(result.ToList().Max(a => a.Count), 2); + } + } +} \ No newline at end of file From 65f72aba36f34103706d15aa2ba0fdb20c61c29a Mon Sep 17 00:00:00 2001 From: Varna Suresh Date: Sun, 7 Oct 2018 00:31:21 +0530 Subject: [PATCH 5/5] Revert "ToLookup method and tests" This reverts commit 36a4b8808b8dd7e6bbd3e69741041d90471c184d. --- ShittyLINQ/ToLookup.cs | 105 ------------------------------- ShittyLinqTests/ToLookupTests.cs | 42 ------------- 2 files changed, 147 deletions(-) delete mode 100644 ShittyLINQ/ToLookup.cs delete mode 100644 ShittyLinqTests/ToLookupTests.cs diff --git a/ShittyLINQ/ToLookup.cs b/ShittyLINQ/ToLookup.cs deleted file mode 100644 index 3d14978..0000000 --- a/ShittyLINQ/ToLookup.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ShittyLINQ -{ - public static partial class Extensions - { - /// - /// Creates a lookup from an IEnumerable - /// - /// Set of elements to be converted to lookup - /// Key selector to determine the key of the lookup - /// Source type - /// Key type - /// A lookup made from source - /// If source, keySelector or element Selector is null - public static Dictionary> ToLookup( - this IEnumerable source, - Func keySelector) - { - return ToLookup(source, keySelector, t => t, EqualityComparer.Default); - } - - /// - /// Creates a lookup from an IEnumerable - /// - /// Set of elements to be converted to lookup - /// Key selector to determine the key of the lookup - /// Element selector to determine the value of the lookup - /// Source type - /// Key type - /// Value type - /// A lookup made from source - /// If source, keySelector or element Selector is null - public static Dictionary> ToLookup( - this IEnumerable source, - Func keySelector, - Func elementSelector) - { - return source.ToLookup(keySelector, elementSelector, EqualityComparer.Default); - } - - /// - /// Creates a lookup from an IEnumerable - /// - /// Set of elements to be converted to lookup - /// Key selector to determine the key of the lookup - /// Comparer to be used to determine equality between elements - /// Source type - /// Key type - /// A lookup made from source - /// If source, keySelector or element Selector is null - public static Dictionary> ToLookup( - this IEnumerable source, - Func keySelector, - IEqualityComparer comparer) - { - return ToLookup(source, keySelector, t => t, comparer); - } - - /// - /// Creates a lookup from an IEnumerable - /// - /// Set of elements to be converted to lookup - /// Key selector to determine the key of the lookup - /// Element selector to determine the value of the lookup - /// Comparer to be used to determine equality between elements - /// Source type - /// Key type - /// Value type - /// A lookup made from source - /// If source, keySelector or element Selector is null - public static Dictionary> ToLookup( - this IEnumerable source, - Func keySelector, - Func elementSelector, - IEqualityComparer comparer) - { - if (source == null || keySelector == null || elementSelector == null) throw new ArgumentNullException(); - - List tempList; - IEnumerable existingValues; - - Dictionary> ret = new Dictionary>(comparer ?? EqualityComparer.Default); - foreach (TSource item in source) - { - existingValues = ret.GetValueOrDefault(keySelector(item)); - if (existingValues == null) - { - tempList = new List(); - } - else - { - tempList = existingValues.ToList(); - ret.Remove(keySelector(item)); - } - tempList.Add(elementSelector(item)); - ret.Add(keySelector(item), tempList); - - } - return ret; - } - } - -} \ No newline at end of file diff --git a/ShittyLinqTests/ToLookupTests.cs b/ShittyLinqTests/ToLookupTests.cs deleted file mode 100644 index cc38edb..0000000 --- a/ShittyLinqTests/ToLookupTests.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using ShittyLINQ; -using ShittyTests.TestHelpers; - -namespace ShittyTests -{ - [TestClass] - public class ToLookupTests - { - [TestMethod] - public void ToLookup_Numbers() - { - IEnumerable numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - - var result = numbers.ToLookup(x => x, x => true); - - Assert.AreEqual(result.Count, 9); - - foreach (int number in numbers) - { - Assert.IsTrue(result.ContainsKey(number)); - } - } - - [TestMethod] - public void ToLookup_Person() - { - var adam = new Person("Adam", 20, "Arquitech", "Amber"); - var brian = new Person("Brian", 45, "Arquitech", "Blue"); - var charles = new Person("Charles", 33, "Arquitech", "Cyan"); - var dani = new Person("Dani", 33, "Developer", "Deep Purple"); - - IEnumerable people = new[] { adam, brian, charles, dani }; - - var result = people.ToLookup(person => person.Age); - - Assert.AreEqual(result.TryGetValue(brian.Age, out var expectedBrian), true); - Assert.IsNotNull(expectedBrian); - } - } -} \ No newline at end of file