Beispiel #1
0
            public override async Task <Terminator <Reduction> > InvokeAsync(Reduction reduction, T value)
            {
                var terminator = await Next.InvokeAsync(reduction, value);

                if (Test(value))
                {
                    return(Terminator.Termination(terminator.Value));
                }

                return(terminator);
            }
Beispiel #2
0
            public override Terminator <Reduction> Invoke(Reduction reduction, T value)
            {
                var terminator = Next.Invoke(reduction, value);

                if (Test(value))
                {
                    return(Terminator.Termination(terminator.Value));
                }

                return(terminator);
            }
Beispiel #3
0
                private bool CheckTermination(Terminator <TReduction> terminator)
                {
                    if (terminator.IsTerminated)
                    {
                        foreach (var reducer in Splitter.Reducers)
                        {
                            reducer.IsTerminated = true;
                        }

                        return(true);
                    }

                    return(false);
                }
Beispiel #4
0
        /// <summary>
        /// Pulls values from the supplied TextReader and passes them into the supplied reducer.
        /// </summary>
        /// <typeparam name="TReduction">The type of the reduction.</typeparam>
        /// <param name="reader">The reader.</param>
        /// <param name="reduction">The reduction.</param>
        /// <param name="reducer">The reducer.</param>
        /// <returns>A wrapped reduction.</returns>
        public static Terminator <TReduction> Reduce <TReduction>(
            this TextReader reader,
            TReduction reduction,
            IReducer <TReduction, string> reducer)
        {
            var terminator = Terminator.Reduction(reduction);

            for (var value = reader.ReadLine(); value != null; value = reader.ReadLine())
            {
                terminator = reducer.Invoke(terminator.Value, value);
                if (terminator.IsTerminated)
                {
                    return(terminator);
                }
            }

            return(reducer.Complete(reduction));
        }
Beispiel #5
0
        public static async Task <Terminator <TReduction> > ReduceAsync <TInput, TReduction>(
            this IEnumerable <TInput> input,
            TReduction reduction,
            IAsyncReducer <TReduction, TInput> reducer)
        {
            var terminator = Terminator.Reduction(reduction);

            foreach (var value in input)
            {
                terminator = await reducer.InvokeAsync(terminator.Value, value).ConfigureAwait(false);

                if (terminator.IsTerminated)
                {
                    return(terminator);
                }
            }

            return(await reducer.CompleteAsync(terminator.Value).ConfigureAwait(false));
        }
Beispiel #6
0
        /// <summary>
        /// Passes elements from an IEnumerable into the supplied reducer.
        /// </summary>
        /// <typeparam name="TInput">The type of the input.</typeparam>
        /// <typeparam name="TReduction">The type of the reduction.</typeparam>
        /// <param name="input">The input.</param>
        /// <param name="reduction">The reduction.</param>
        /// <param name="reducer">The reducer.</param>
        /// <returns>A terminated reduction.</returns>
        public static Terminator <TReduction> Reduce <TInput, TReduction>(
            this IEnumerable <TInput> input,
            TReduction reduction,
            IReducer <TReduction, TInput> reducer)
        {
            var terminator = Terminator.Reduction(reduction);

            foreach (var value in input)
            {
                terminator = reducer.Invoke(terminator.Value, value);

                if (terminator.IsTerminated)
                {
                    return(terminator);
                }
            }

            return(reducer.Complete(terminator.Value));
        }