From 5a64a6c26941886c4c29397489355253f8a126b7 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 25 Sep 2019 13:52:02 -0400 Subject: [PATCH] refactor: Tidying --- .../Extensions/EnumerableExtensions.cs | 106 +++++++----------- ICD.Common.Utils/ReflectionUtils.cs | 3 - 2 files changed, 40 insertions(+), 69 deletions(-) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index ae1de2e..6c977cb 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -732,6 +732,22 @@ namespace ICD.Common.Utils.Extensions return extends.Where(i => !comparer.Equals(item, i)); } + /// + /// Removes any null elements from an enumerable of nullable value types + /// + /// + /// + /// + [PublicAPI] + public static IEnumerable ExceptNulls(this IEnumerable extends) + where T : struct + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return extends.OfType(); + } + /// /// Copies all the elements of the current one-dimensional array to the specified one-dimensional array /// starting at the specified destination array index. The index is specified as a 32-bit integer. @@ -1241,22 +1257,6 @@ namespace ICD.Common.Utils.Extensions } } - /// - /// Removes any null elements from an enumerable of nullable value types - /// - /// - /// - /// - [PublicAPI] - public static IEnumerable ExceptNulls(this IEnumerable extends) - where T : struct - { - if (extends == null) - throw new ArgumentNullException("extends"); - - return extends.OfType(); - } - /// /// Computes the sum of a sequence of byte values. /// @@ -1369,19 +1369,39 @@ namespace ICD.Common.Utils.Extensions return any; } + /// + /// Combines the corresponding elements of two sequences, producing a sequence of KeyValuePairs. + /// + /// + /// + /// + /// + public static IEnumerable> Zip(this IEnumerable extends, + IEnumerable second) + { + if (extends == null) + throw new ArgumentNullException("first"); + + if (second == null) + throw new ArgumentNullException("second"); + + return extends.Zip(second, (f, s) => new KeyValuePair(f, s)); + } + +#if SIMPLSHARP /// /// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results. /// /// /// - /// + /// /// /// - public static void Zip(this IEnumerable first, + public static void Zip(this IEnumerable extends, IEnumerable second, Action callback) { - if (first == null) + if (extends == null) throw new ArgumentNullException("first"); if (second == null) @@ -1390,7 +1410,7 @@ namespace ICD.Common.Utils.Extensions if (callback == null) throw new ArgumentNullException("callback"); - using (IEnumerator enumerator1 = first.GetEnumerator()) + using (IEnumerator enumerator1 = extends.GetEnumerator()) { using (IEnumerator enumerator2 = second.GetEnumerator()) { @@ -1400,51 +1420,6 @@ namespace ICD.Common.Utils.Extensions } } - // since S# can't do anonymous types - private struct TryParseStruct - { - public readonly T value; - public readonly bool isParsed; - - public TryParseStruct(T value, bool isParsed) - { - this.value = value; - this.isParsed = isParsed; - } - } - - // since Func<...,T> can't specify `out` parameters - public delegate bool TryParseDelegate(string input, out T output); - - /// - /// Attempts to parse each value of the enumerable, - /// throwing away the values that don't parse correctly. - /// - /// type to parse to - /// enumerable of strings to parse - /// TryParse function for given type - /// enumerable of successfully parsed values - public static IEnumerable TryParseSkipFailures(this IEnumerable extends, - TryParseDelegate tryParseFunc) - { - if (extends == null) - throw new ArgumentNullException("extends"); - - if (tryParseFunc == null) - throw new ArgumentNullException("tryParseFunc"); - - return extends.Select(str => - { - T value; - bool isParsed = tryParseFunc(str, out value); - return new TryParseStruct(value, isParsed); - }) - .Where(v => v.isParsed) - .Select(v => v.value); - } - -#if SIMPLSHARP - /// /// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results. /// @@ -1494,7 +1469,6 @@ namespace ICD.Common.Utils.Extensions } } } - #endif } } diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index fac658c..899dcd3 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -222,13 +222,10 @@ namespace ICD.Common.Utils throw new ArgumentNullException("parameters"); try { - if (parameters.Length == 0) return Activator.CreateInstance(type); ConstructorInfo constructor = GetConstructor(type, parameters); - - return constructor.Invoke(parameters); } catch (TypeLoadException e)