Exemple #1
0
        public static T2 WhereSumF <T2>(this ReadOnlySpan <float> source, Func <float, bool> predicate, Func <float, T2> selector)
            where T2 : struct, IConvertible // Make sure these are not nullable
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (predicate == null)
            {
                throw Error.ArgumentNull(nameof(predicate));
            }
            if (selector == null)
            {
                throw Error.ArgumentNull(nameof(selector));
            }

            INumericPolicy <double> p = NumericPolicies.Instance;
            double a = 0;

            checked
            {
                foreach (float b in source)
                {
                    if (predicate(b))
                    {
                        a = p.Add(a, selector(b).ToDouble(CultureInfo.InvariantCulture));
                    }
                }
            }

            return((T2)Convert.ChangeType(a, typeof(T2)));
        }
Exemple #2
0
        public static T2 WhereSumF <T2>(this List <float?> source, Func <float?, bool> predicate, Func <float?, T2> selector)
            where T2 : struct, IConvertible // Make sure these are not nullable
        {
            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }
            if (predicate == null)
            {
                throw Error.ArgumentNull(nameof(predicate));
            }

            if (selector == null)
            {
                throw Error.ArgumentNull(nameof(selector));
            }

            INumericPolicy <double> p = NumericPolicies.Instance;
            double a = 0;

            checked
            {
                for (int index = 0; index < source.Count; index++)
                {
                    if (predicate(source[index]))
                    {
                        a = p.Add(a, selector(source[index]).ToDouble(CultureInfo.InvariantCulture));
                    }
                }
            }

            return((T2)Convert.ChangeType(a, typeof(T2)));
        }