From ee670487ddc19cc184c94dada87f02f6c73cda46 Mon Sep 17 00:00:00 2001 From: Austin Noska Date: Wed, 17 Jun 2020 14:05:54 -0400 Subject: [PATCH] feat: Added AggregateOrDefault extension method --- CHANGELOG.md | 1 + .../Extensions/EnumerableExtensions.cs | 77 +++++++++++++------ 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e603e83..37d2802 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - Added attributes for controlling obfuscation + - Added AggregateOrDefault extension method for applying an accumulator function over a sequence that returns a default value if the sequence is empty ## [12.0.0] - 2020-06-18 ### Added diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index c6eec3e..4a541e6 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -1407,24 +1407,7 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - Comparer comparer = Comparer.Default; - T value = default(T); - bool first = true; - - foreach (T x in extends) - { - if (first) - { - first = false; - value = x; - continue; - } - - if (comparer.Compare(x, value) < 0) - value = x; - } - - return value; + return extends.AggregateOrDefault((a, b) => Comparer.Default.Compare(a, b) < 0 ? a : b); } /// @@ -1438,16 +1421,60 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - Comparer comparer = Comparer.Default; - T value = default(T); + return extends.AggregateOrDefault((a, b) => Comparer.Default.Compare(a, b) > 0 ? a : b); + } - foreach (T x in extends) + /// + /// Applies an accumulator function over a sequence. + /// + /// + /// + /// + /// The final accumulator value. + [CanBeNull] + public static TSource AggregateOrDefault([NotNull] this IEnumerable extends, + [NotNull] Func func) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (func == null) + throw new ArgumentNullException("func"); + + return extends.AggregateOrDefault(func, default(TSource)); + } + + /// + /// Applies an accumulator function over a sequence. + /// + /// + /// + /// + /// + /// The final accumulator value. + [CanBeNull] + public static TSource AggregateOrDefault([NotNull] this IEnumerable extends, + [NotNull] Func func, + TSource defaultValue) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (func == null) + throw new ArgumentNullException("func"); + + using (IEnumerator enumerator = extends.GetEnumerator()) { - if (comparer.Compare(x, value) > 0) - value = x; - } + if (!enumerator.MoveNext()) + return defaultValue; - return value; + TSource result = enumerator.Current; + + while (enumerator.MoveNext()) + result = func(result, enumerator.Current); + + return result; + } } ///