mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Added AggregateOrDefault extension method
This commit is contained in:
committed by
Chris Cameron
parent
4f34b3da7c
commit
ee670487dd
@@ -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
|
||||
|
||||
@@ -1407,24 +1407,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
Comparer<T> comparer = Comparer<T>.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<T>.Default.Compare(a, b) < 0 ? a : b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1438,16 +1421,60 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
Comparer<T> comparer = Comparer<T>.Default;
|
||||
T value = default(T);
|
||||
|
||||
foreach (T x in extends)
|
||||
{
|
||||
if (comparer.Compare(x, value) > 0)
|
||||
value = x;
|
||||
return extends.AggregateOrDefault((a, b) => Comparer<T>.Default.Compare(a, b) > 0 ? a : b);
|
||||
}
|
||||
|
||||
return value;
|
||||
/// <summary>
|
||||
/// Applies an accumulator function over a sequence.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="func"></param>
|
||||
/// <returns>The final accumulator value.</returns>
|
||||
[CanBeNull]
|
||||
public static TSource AggregateOrDefault<TSource>([NotNull] this IEnumerable<TSource> extends,
|
||||
[NotNull] Func<TSource, TSource, TSource> func)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (func == null)
|
||||
throw new ArgumentNullException("func");
|
||||
|
||||
return extends.AggregateOrDefault(func, default(TSource));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies an accumulator function over a sequence.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="func"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns>The final accumulator value.</returns>
|
||||
[CanBeNull]
|
||||
public static TSource AggregateOrDefault<TSource>([NotNull] this IEnumerable<TSource> extends,
|
||||
[NotNull] Func<TSource, TSource, TSource> func,
|
||||
TSource defaultValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (func == null)
|
||||
throw new ArgumentNullException("func");
|
||||
|
||||
using (IEnumerator<TSource> enumerator = extends.GetEnumerator())
|
||||
{
|
||||
if (!enumerator.MoveNext())
|
||||
return defaultValue;
|
||||
|
||||
TSource result = enumerator.Current;
|
||||
|
||||
while (enumerator.MoveNext())
|
||||
result = func(result, enumerator.Current);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user