Ejemplo n.º 1
0
        /// <summary>
        /// Applies an accumulator function over a sequence. The specified seed value is used as the
        /// initial accumulator value.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
        /// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
        /// <param name="source">An <see cref="IBindableCollection{TElement}"/> to aggregate over.</param>
        /// <param name="seed">The initial accumulator value.</param>
        /// <param name="func">An accumulator function to be invoked on each element.</param>
        /// <param name="dependencyAnalysisMode">The dependency analysis mode.</param>
        /// <returns>The final accumulator value.</returns>
        public static IBindable <TAccumulate> Aggregate <TSource, TAccumulate>(this IBindableCollection <TSource> source, TAccumulate seed, Expression <Func <TAccumulate, TSource, TAccumulate> > func, DependencyDiscovery dependencyAnalysisMode)
        {
            source.ShouldNotBeNull("source");
            func.ShouldNotBeNull("func");
            seed.ShouldNotBeNull("seed");

            var compiledAccumulator = func.Compile();
            var function            = new Func <IBindableCollection <TSource>, TAccumulate>(
                sourceElements =>
            {
                var current = seed;
                foreach (var sourceElement in sourceElements)
                {
                    current = compiledAccumulator(current, sourceElement);
                }
                return(current);
            }
                );

            var result = new CustomAggregator <TSource, TAccumulate>(source, function, source.Dispatcher);

            if (dependencyAnalysisMode == DependencyDiscovery.Enabled)
            {
                return(result.DependsOnExpression(func, func.Parameters[1]));
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Applies an accumulator function over a sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="source">An <see cref="IBindableCollection{TSource}"/> to aggregate over.</param>
        /// <param name="func">An accumulator function to be invoked on each element.</param>
        /// <param name="dependencyAnalysisMode">The dependency analysis mode.</param>
        /// <returns>The final accumulator value.</returns>
        public static IBindable <TResult> Aggregate <TSource, TResult>(this IBindableCollection <TSource> source, Expression <Func <IBindableCollection <TSource>, TResult> > func, DependencyDiscovery dependencyAnalysisMode)
        {
            source.ShouldNotBeNull("source");
            func.ShouldNotBeNull("func");
            var result = new CustomAggregator <TSource, TResult>(source, func.Compile(), source.Dispatcher);

            if (dependencyAnalysisMode == DependencyDiscovery.Enabled)
            {
                return(result.DependsOnExpression(func, func.Parameters[0]));
            }
            return(result);
        }