예제 #1
0
        /// <summary>
        /// Safely aggregates the items emitted by the given pipe. If the pipe is null
        /// or has no elements the default value will be returned.
        /// </summary>
        /// <typeparam name="T">The type of the items emitted by the pipe.</typeparam>
        /// <param name="SourcePipe">A pipe.</param>
        /// <param name="DefaultValue">The default value to return for an empty pipe.</param>
        public static T Aggregate <T>(this IEndPipe <T> SourcePipe,
                                      T Prefix,
                                      Func <T, T> Map,
                                      Func <T, T, T> Reduce,
                                      T Suffix,
                                      T DefaultValue = default(T))
        {
            if (SourcePipe == null)
            {
                return(DefaultValue);
            }

            try
            {
                return(Reduce(Reduce(Prefix, SourcePipe.Select(Item => Map(Item)).Aggregate(Reduce)), Suffix));
            }
            catch (Exception e)
            {
                return(DefaultValue);
            }
        }