예제 #1
0
        public static IEnumerable <T> Repeat <T>(T element, int count)
        {
            if (count < 0)
            {
                throw EnumerableExtra.CreateOutOfRange("count");
            }

            return(RepeatHelper(element, count));
        }
예제 #2
0
        public static IEnumerable <int> Range(int start, int count)
        {
            long test = (start + (long)count) - 1L;

            if (count < 0 || test > (long)Int32.MaxValue)
            {
                throw EnumerableExtra.CreateOutOfRange("count");
            }

            return(RangeHelper(start, count));
        }
예제 #3
0
        public static T First <T>(this IEnumerable <T> enumerable)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException("enumerable");
            }

            using (var e = enumerable.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    throw EnumerableExtra.CreateEmpty();
                }

                return(e.Current);
            }
        }
예제 #4
0
        private static T AggregateHelper <T>(this IEnumerable <T> source, Func <T, T, T> func)
        {
            using (var e = source.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    throw EnumerableExtra.CreateEmpty();
                }

                var result = e.Current;
                while (e.MoveNext())
                {
                    result = func(result, e.Current);
                }

                return(result);
            }
        }
예제 #5
0
 public static double Max(this IEnumerable <double> e)
 {
     if (e == null)
     {
         throw new ArgumentNullException("e");
     }
     using (var enumerator = e.GetEnumerator()) {
         if (!enumerator.MoveNext())
         {
             throw EnumerableExtra.CreateEmpty();
         }
         var max = enumerator.Current;
         while (enumerator.MoveNext())
         {
             if (enumerator.Current > max)
             {
                 max = enumerator.Current;
             }
         }
         return(max);
     }
 }
예제 #6
0
 public static long Min(this IEnumerable <long> e)
 {
     if (e == null)
     {
         throw new ArgumentNullException("e");
     }
     using (var enumerator = e.GetEnumerator()) {
         if (!enumerator.MoveNext())
         {
             throw EnumerableExtra.CreateEmpty();
         }
         var min = enumerator.Current;
         while (enumerator.MoveNext())
         {
             if (enumerator.Current < min)
             {
                 min = enumerator.Current;
             }
         }
         return(min);
     }
 }
예제 #7
0
        public static T ElementAt <T>(this IEnumerable <T> enumerable, int count)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException("enumerable");
            }

            using (var e = enumerable.GetEnumerator())
            {
                int cur = 0;
                do
                {
                    if (!e.MoveNext())
                    {
                        throw EnumerableExtra.CreateOutOfRange("count");
                    }
                    cur++;
                }while (cur <= count);

                return(e.Current);
            }
        }
예제 #8
0
        public static T First <T>(this IEnumerable <T> enumerable, Func <T, bool> predicate)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException("enumerable");
            }


            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }

            foreach (var cur in enumerable)
            {
                if (predicate(cur))
                {
                    return(cur);
                }
            }

            throw EnumerableExtra.CreateNoMatch();
        }