Merge pull request #829 from symform/httpwebreq-async-ssl
[mono.git] / mcs / class / System.Core / System.Linq / Enumerable.cs
index 2b44f9f420e9bb3d304930668b2a5c63772d7a37..23805636c6b6236d00dc7b20fce0feeb83ffd659 100644 (file)
@@ -45,9 +45,11 @@ namespace System.Linq
                        Throw
                }
 
+#if !FULL_AOT_RUNTIME
                static class PredicateOf<T> {
                        public static readonly Func<T, bool> Always = (t) => true;
                }
+#endif
 
                static class Function<T> {
                        public static readonly Func<T, T> Identity = (t) => t;
@@ -71,7 +73,7 @@ namespace System.Linq
                        // if zero elements and treat the first element differently
                        using (var enumerator = source.GetEnumerator ()) {
                                if (!enumerator.MoveNext ())
-                                       throw new InvalidOperationException ("No elements in source list");
+                                       throw EmptySequence ();
 
                                TSource folded = enumerator.Current;
                                while (enumerator.MoveNext ())
@@ -162,48 +164,77 @@ namespace System.Linq
 
                public static double Average (this IEnumerable<int> source)
                {
-                       return Average<int, long, double> (source, (a, b) => a + b, (a, b) => (double) a / (double) b);
+                       Check.Source (source);
+
+                       long total = 0;
+                       int count = 0;
+                       foreach (var element in source){
+                               total = checked (total + element);
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / (double) count;
                }
 
                public static double Average (this IEnumerable<long> source)
                {
-                       return Average<long, long, double> (source, (a, b) => a + b, (a, b) => (double) a / (double) b);
+                       Check.Source (source);
+
+                       long total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += element;
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / (double) count;
                }
 
                public static double Average (this IEnumerable<double> source)
                {
-                       return Average<double, double, double> (source, (a, b) => a + b, (a, b) => a / b);
+                       Check.Source (source);
+
+                       double total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += element;
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / count;
                }
 
                public static float Average (this IEnumerable<float> source)
                {
-                       return Average<float, double, float> (source, (a, b) => a + b, (a, b) => (float) a / (float) b);
-               }
+                       Check.Source (source);
 
-               public static decimal Average (this IEnumerable<decimal> source)
-               {
-                       return Average<decimal, decimal, decimal> (source, (a, b) => a + b, (a, b) => a / b);
+                       float total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += element;
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / count;
                }
 
-               static TResult Average<TElement, TAggregate, TResult> (this IEnumerable<TElement> source,
-                       Func<TAggregate, TElement, TAggregate> func, Func<TAggregate, long, TResult> result)
-                       where TElement : struct
-                       where TAggregate : struct
-                       where TResult : struct
+               public static decimal Average (this IEnumerable<decimal> source)
                {
                        Check.Source (source);
 
-                       var total = default (TAggregate);
-                       long counter = 0;
-                       foreach (var element in source) {
-                               total = func (total, element);
-                               ++counter;
+                       decimal total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += element;
+                               count++;
                        }
-
-                       if (counter == 0)
-                               throw new InvalidOperationException ();
-
-                       return result (total, counter);
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / count;
                }
 
                static TResult? AverageNullable<TElement, TAggregate, TResult> (this IEnumerable<TElement?> source,
@@ -234,105 +265,297 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return source.AverageNullable<int, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+                       long total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               total = total + element.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new double? (total / (double) counter);
                }
 
                public static double? Average (this IEnumerable<long?> source)
                {
                        Check.Source (source);
 
-                       return source.AverageNullable<long, long, double> ((a, b) => a + b, (a, b) => (double) a / b);
+                       long total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               total = checked (total + element.Value);
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new double? (total / (double) counter);
+
                }
 
                public static double? Average (this IEnumerable<double?> source)
                {
                        Check.Source (source);
 
-                       return source.AverageNullable<double, double, double> ((a, b) => a + b, (a, b) => a / b);
+                       double total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               total = total + element.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new double? (total / counter);
+
                }
 
                public static decimal? Average (this IEnumerable<decimal?> source)
                {
                        Check.Source (source);
 
-                       return source.AverageNullable<decimal, decimal, decimal> ((a, b) => a + b, (a, b) => a / b);
+                       decimal total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               total = total + element.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new decimal? (total / counter);
+
                }
 
                public static float? Average (this IEnumerable<float?> source)
                {
                        Check.Source (source);
 
-                       return source.AverageNullable<float, double, float> ((a, b) => a + b, (a, b) => (float) a / (float) b);
+                       float total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               total = total + element.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new float? (total / counter);
+
                }
 
                public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).Average<int, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+                       long total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += selector (element);
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / (double) count;
                }
 
                public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).AverageNullable<int, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+                       long total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (!value.HasValue)
+                                       continue;
+
+                               total = total + value.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new double? (total / (double) counter);
                }
 
                public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).Average<long, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+                       long total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total = checked (total + selector (element));
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / (double) count;
+
                }
 
                public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).AverageNullable<long, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+                       long total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (!value.HasValue)
+                                       continue;
+
+                               total = checked (total + value.Value);
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new double? (total / (double) counter);
                }
 
                public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).Average<double, double, double> ((a, b) => a + b, (a, b) => a / b);
+                       double total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += selector (element);
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / count;
+
                }
 
                public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).AverageNullable<double, double, double> ((a, b) => a + b, (a, b) => a / b);
+                       double total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (!value.HasValue)
+                                       continue;
+
+                               total = total + value.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new double? (total / counter);
+
                }
 
                public static float Average<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).Average<float, double, float> ((a, b) => a + b, (a, b) => (float) a / (float) b);
+                       float total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += selector (element);
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / count;
                }
 
                public static float? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).AverageNullable<float, double, float> ((a, b) => a + b, (a, b) => (float) a / (float) b);
+                       float total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (!value.HasValue)
+                                       continue;
+
+                               total = total + value.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new float? (total / counter);
                }
 
                public static decimal Average<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).Average<decimal, decimal, decimal> ((a, b) => a + b, (a, b) => a / b);
+                       decimal total = 0;
+                       long count = 0;
+                       foreach (var element in source){
+                               total += selector (element);
+                               count++;
+                       }
+                       if (count == 0)
+                               throw EmptySequence ();
+                       return total / count;
                }
 
                public static decimal? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.Select (selector).AverageNullable<decimal, decimal, decimal> ((a, b) => a + b, (a, b) => a / b);
+                       decimal total = 0;
+                       long counter = 0;
+                       
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (!value.HasValue)
+                                       continue;
+
+                               total = total + value.Value;
+                               counter++;
+                       }
+
+                       if (counter == 0)
+                               return null;
+
+                       return new decimal? (total / counter);
                }
 
                #endregion
@@ -416,19 +639,19 @@ namespace System.Linq
                        int counter = 0;
                        using (var enumerator = source.GetEnumerator ())
                                while (enumerator.MoveNext ())
-                                       counter++;
+                                       checked { counter++; }
 
                        return counter;
                }
 
-               public static int Count<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> selector)
+               public static int Count<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
                {
-                       Check.SourceAndSelector (source, selector);
+                       Check.SourceAndSelector (source, predicate);
 
                        int counter = 0;
                        foreach (var element in source)
-                               if (selector (element))
-                                       counter++;
+                               if (predicate (element))
+                                       checked { counter++; }
 
                        return counter;
                }
@@ -521,6 +744,12 @@ namespace System.Linq
                        if (list != null)
                                return list [index];
 
+#if NET_4_5
+                       var readOnlyList = source as IReadOnlyList<TSource>;
+                       if (readOnlyList != null)
+                               return readOnlyList[index];
+#endif
+
                        return source.ElementAt (index, Fallback.Throw);
                }
 
@@ -539,6 +768,12 @@ namespace System.Linq
                        if (list != null)
                                return index < list.Count ? list [index] : default (TSource);
 
+#if NET_4_5
+                       var readOnlyList = source as IReadOnlyList<TSource>;
+                       if (readOnlyList != null)
+                               return index < readOnlyList.Count ? readOnlyList [index] : default (TSource);
+#endif
+
                        return source.ElementAt (index, Fallback.Default);
                }
 
@@ -574,7 +809,7 @@ namespace System.Linq
                {
                        var items = new HashSet<TSource> (second, comparer);
                        foreach (var element in first) {
-                               if (!items.Contains (element, comparer))
+                               if (items.Add (element))
                                        yield return element;
                        }
                }
@@ -590,7 +825,7 @@ namespace System.Linq
                                        return element;
 
                        if (fallback == Fallback.Throw)
-                               throw new InvalidOperationException ();
+                               throw NoMatchingElement ();
 
                        return default (TSource);
                }
@@ -610,7 +845,7 @@ namespace System.Linq
                                }
                        }
 
-                       throw new InvalidOperationException ("The source sequence is empty");
+                       throw EmptySequence ();
                }
 
                public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
@@ -628,7 +863,15 @@ namespace System.Linq
                {
                        Check.Source (source);
 
+#if !FULL_AOT_RUNTIME
                        return source.First (PredicateOf<TSource>.Always, Fallback.Default);
+#else
+                       // inline the code to reduce dependency o generic causing AOT errors on device (e.g. bug #3285)
+                       foreach (var element in source)
+                               return element;
+
+                       return default (TSource);
+#endif
                }
 
                public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
@@ -670,8 +913,8 @@ namespace System.Linq
                static IEnumerable<IGrouping<TKey, TSource>> CreateGroupByIterator<TSource, TKey> (this IEnumerable<TSource> source,
                        Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
                {
-                       Dictionary<TKey, List<TSource>> groups = new Dictionary<TKey, List<TSource>> ();
-                       List<TSource> nullList = new List<TSource> ();
+                       var groups = new Dictionary<TKey, List<TSource>> ();
+                       var nullList = new List<TSource> ();
                        int counter = 0;
                        int nullCounter = -1;
 
@@ -695,14 +938,18 @@ namespace System.Linq
                        }
 
                        counter = 0;
-                       foreach (KeyValuePair<TKey, List<TSource>> group in groups) {
+                       foreach (var group in groups) {
                                if (counter == nullCounter) {
-                                       Grouping<TKey, TSource> nullGroup = new Grouping<TKey, TSource> (default (TKey), nullList);
-                                       yield return nullGroup;
+                                       yield return new Grouping<TKey, TSource> (default (TKey), nullList);
                                        counter++;
                                }
-                               Grouping<TKey, TSource> grouping = new Grouping<TKey, TSource> (group.Key, group.Value);
-                               yield return grouping;
+
+                               yield return new Grouping<TKey, TSource> (group.Key, group.Value);
+                               counter++;
+                       }
+
+                       if (counter == nullCounter) {
+                               yield return new Grouping<TKey, TSource> (default (TKey), nullList);
                                counter++;
                        }
                }
@@ -718,8 +965,14 @@ namespace System.Linq
                {
                        Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
 
-                       Dictionary<TKey, List<TElement>> groups = new Dictionary<TKey, List<TElement>> ();
-                       List<TElement> nullList = new List<TElement> ();
+                       return CreateGroupByIterator (source, keySelector, elementSelector, comparer);
+               }
+
+               static IEnumerable<IGrouping<TKey, TElement>> CreateGroupByIterator<TSource, TKey, TElement> (this IEnumerable<TSource> source,
+                       Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
+               {
+                       var groups = new Dictionary<TKey, List<TElement>> ();
+                       var nullList = new List<TElement> ();
                        int counter = 0;
                        int nullCounter = -1;
 
@@ -744,14 +997,18 @@ namespace System.Linq
                        }
 
                        counter = 0;
-                       foreach (KeyValuePair<TKey, List<TElement>> group in groups) {
+                       foreach (var group in groups) {
                                if (counter == nullCounter) {
-                                       Grouping<TKey, TElement> nullGroup = new Grouping<TKey, TElement> (default (TKey), nullList);
-                                       yield return nullGroup;
+                                       yield return new Grouping<TKey, TElement> (default (TKey), nullList);
                                        counter++;
                                }
-                               Grouping<TKey, TElement> grouping = new Grouping<TKey, TElement> (group.Key, group.Value);
-                               yield return grouping;
+
+                               yield return new Grouping<TKey, TElement> (group.Key, group.Value);
+                               counter++;
+                       }
+
+                       if (counter == nullCounter) {
+                               yield return new Grouping<TKey, TElement> (default (TKey), nullList);
                                counter++;
                        }
                }
@@ -767,6 +1024,16 @@ namespace System.Linq
                        Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
                        Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
                        IEqualityComparer<TKey> comparer)
+               {
+                       Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
+
+                       return CreateGroupByIterator (source, keySelector, elementSelector, resultSelector, comparer);
+               }
+
+               static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
+                       Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
+                       Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
+                       IEqualityComparer<TKey> comparer)
                {
                        IEnumerable<IGrouping<TKey, TElement>> groups = GroupBy<TSource, TKey, TElement> (
                                source, keySelector, elementSelector, comparer);
@@ -786,6 +1053,16 @@ namespace System.Linq
                        Func<TSource, TKey> keySelector,
                        Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
                        IEqualityComparer<TKey> comparer)
+               {
+                       Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
+
+                       return CreateGroupByIterator (source, keySelector, resultSelector, comparer);
+               }
+
+               static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TResult> (this IEnumerable<TSource> source,
+                       Func<TSource, TKey> keySelector,
+                       Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
+                       IEqualityComparer<TKey> comparer)
                {
                        IEnumerable<IGrouping<TKey,TSource>> groups = GroupBy<TSource, TKey> (source, keySelector, comparer);
 
@@ -834,7 +1111,7 @@ namespace System.Linq
 
                        foreach (TOuter element in outer) {
                                TKey outerKey = outerKeySelector (element);
-                               if (innerKeys.Contains (outerKey))
+                               if (outerKey != null && innerKeys.Contains (outerKey))
                                        yield return resultSelector (element, innerKeys [outerKey]);
                                else
                                        yield return resultSelector (element, Empty<TInner> ());
@@ -901,7 +1178,7 @@ namespace System.Linq
 
                        foreach (TOuter element in outer) {
                                TKey outerKey = outerKeySelector (element);
-                               if (innerKeys.Contains (outerKey)) {
+                               if (outerKey != null && innerKeys.Contains (outerKey)) {
                                        foreach (TInner innerElement in innerKeys [outerKey])
                                                yield return resultSelector (element, innerElement);
                                }
@@ -936,7 +1213,7 @@ namespace System.Linq
                                return item;
 
                        if (fallback == Fallback.Throw)
-                               throw new InvalidOperationException ();
+                               throw NoMatchingElement ();
 
                        return item;
                }
@@ -947,14 +1224,29 @@ namespace System.Linq
 
                        var collection = source as ICollection<TSource>;
                        if (collection != null && collection.Count == 0)
-                               throw new InvalidOperationException ();
+                               throw EmptySequence ();
 
                        var list = source as IList<TSource>;
                        if (list != null)
                                return list [list.Count - 1];
 
+#if !FULL_AOT_RUNTIME
                        return source.Last (PredicateOf<TSource>.Always, Fallback.Throw);
-               }
+#else
+                       var empty = true;
+                       var item = default (TSource);
+
+                       foreach (var element in source) {
+                               item = element;
+                               empty = false;
+                       }
+
+                       if (!empty)
+                               return item;
+
+                       throw EmptySequence ();
+#endif
+        }
 
                public static TSource Last<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
                {
@@ -975,7 +1267,22 @@ namespace System.Linq
                        if (list != null)
                                return list.Count > 0 ? list [list.Count - 1] : default (TSource);
 
+#if !FULL_AOT_RUNTIME
                        return source.Last (PredicateOf<TSource>.Always, Fallback.Default);
+#else
+                       var empty = true;
+                       var item = default (TSource);
+
+                       foreach (var element in source) {
+                               item = element;
+                               empty = false;
+                       }
+
+                       if (!empty)
+                               return item;
+
+                       return item;
+#endif
                }
 
                public static TSource LastOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
@@ -1007,13 +1314,13 @@ namespace System.Linq
                        return counter;
                }
 
-               public static long LongCount<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> selector)
+               public static long LongCount<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
                {
-                       Check.SourceAndSelector (source, selector);
+                       Check.SourceAndSelector (source, predicate);
 
                        long counter = 0;
                        foreach (TSource element in source)
-                               if (selector (element))
+                               if (predicate (element))
                                        counter++;
 
                        return counter;
@@ -1027,200 +1334,289 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return Iterate (source, int.MinValue, (a, b) => Math.Max (a, b));
+                       bool empty = true;
+                       var max = int.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (element, max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence();
+                       return max;
                }
 
                public static long Max (this IEnumerable<long> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, long.MinValue, (a, b) => Math.Max (a, b));
+                       bool empty = true;
+                       var max = long.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (element, max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return max;
                }
 
                public static double Max (this IEnumerable<double> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, double.MinValue, (a, b) => Math.Max (a, b));
+                       bool empty = true;
+                       var max = double.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (element, max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return max;
                }
 
                public static float Max (this IEnumerable<float> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, float.MinValue, (a, b) => Math.Max (a, b));
+                       bool empty = true;
+                       var max = float.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (element, max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return max;
                }
 
                public static decimal Max (this IEnumerable<decimal> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, decimal.MinValue, (a, b) => Math.Max (a, b));
+                       bool empty = true;
+                       var max = decimal.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (element, max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return max;
                }
 
                public static int? Max (this IEnumerable<int?> source)
                {
                        Check.Source (source);
 
-                       return IterateNullable (source, (a, b) => Math.Max (a, b));
+                       bool empty = true;
+                       var max = int.MinValue;
+                               
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               max = Math.Max (element.Value, max);
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+
+                       return max;
                }
 
                public static long? Max (this IEnumerable<long?> source)
                {
                        Check.Source (source);
 
-                       return IterateNullable (source, (a, b) => Math.Max (a, b));
-               }
-
-               public static double? Max (this IEnumerable<double?> source)
-               {
-                       Check.Source (source);
-
-                       return IterateNullable (source, (a, b) => Math.Max (a, b));
-               }
-
-               public static float? Max (this IEnumerable<float?> source)
-               {
-                       Check.Source (source);
-
-                       return IterateNullable (source, (a, b) => Math.Max (a, b));
-               }
-
-               public static decimal? Max (this IEnumerable<decimal?> source)
-               {
-                       Check.Source (source);
-
-                       return IterateNullable (source, (a, b) => Math.Max (a, b));
-               }
-
-               static T? IterateNullable<T> (IEnumerable<T?> source, Func<T, T, T> selector) where T : struct
-               {
                        bool empty = true;
-                       T? value = null;
+                       var max = long.MinValue;
+                               
                        foreach (var element in source) {
                                if (!element.HasValue)
                                        continue;
 
-                               if (!value.HasValue)
-                                       value = element.Value;
-                               else
-                                       value = selector (element.Value, value.Value);
-
+                               max = Math.Max (element.Value, max);
                                empty = false;
                        }
 
                        if (empty)
                                return null;
 
-                       return value;
+                       return max;
                }
 
-               static TRet? IterateNullable<TSource, TRet> (
-                       IEnumerable<TSource> source,
-                       Func<TSource, TRet?> source_selector,
-                       Func<TRet?, TRet?, bool> selector) where TRet : struct
+               public static double? Max (this IEnumerable<double?> source)
                {
+                       Check.Source (source);
+
                        bool empty = true;
-                       TRet? value = null;
+                       var max = double.MinValue;
+                               
                        foreach (var element in source) {
-                               TRet? item = source_selector (element);
-
-                               if (!value.HasValue)
-                                       value = item;
-                               else if (selector (item, value))
-                                       value = item;
+                               if (!element.HasValue)
+                                       continue;
 
+                               max = Math.Max (element.Value, max);
                                empty = false;
                        }
 
                        if (empty)
                                return null;
 
-                       return value;
+                       return max;
                }
 
-               static TSource IterateNullable<TSource> (IEnumerable<TSource> source, Func<TSource, TSource, bool> selector)
+               public static float? Max (this IEnumerable<float?> source)
                {
-                       var value = default (TSource);
+                       Check.Source (source);
 
+                       bool empty = true;
+                       var max = float.MinValue;
+                               
                        foreach (var element in source) {
-                               if (element == null)
+                               if (!element.HasValue)
                                        continue;
 
-                               if (value == null || selector (element, value))
-                                       value = element;
+                               max = Math.Max (element.Value, max);
+                               empty = false;
                        }
 
-                       return value;
+                       if (empty)
+                               return null;
+
+                       return max;
                }
 
-               static TSource IterateNonNullable<TSource> (IEnumerable<TSource> source, Func<TSource, TSource, bool> selector)
+               public static decimal? Max (this IEnumerable<decimal?> source)
                {
-                       var value = default (TSource);
+                       Check.Source (source);
+
                        bool empty = true;
+                       var max = decimal.MinValue;
+                               
                        foreach (var element in source) {
-                               if (empty) {
-                                       value = element;
-                                       empty = false;
+                               if (!element.HasValue)
                                        continue;
-                               }
 
-                               if (selector (element, value))
-                                       value = element;
+                               max = Math.Max (element.Value, max);
+                               empty = false;
                        }
 
                        if (empty)
-                               throw new InvalidOperationException ();
+                               return null;
 
-                       return value;
+                       return max;
                }
 
+               // TODO: test nullable and non-nullable
                public static TSource Max<TSource> (this IEnumerable<TSource> source)
                {
                        Check.Source (source);
 
                        var comparer = Comparer<TSource>.Default;
-                       Func<TSource, TSource, bool> compare = (a, b) => comparer.Compare (a, b) > 0;
 
-                       if (default (TSource) == null)
-                               return IterateNullable (source, compare);
+                       TSource max = default (TSource);
+                       
+                       if (default (TSource) == null){
+                               foreach (var element in source) {
+                                       if (element == null)
+                                               continue;
 
-                       return IterateNonNullable (source, compare);
+                                       if (max == null || comparer.Compare (element, max) > 0)
+                                               max = element;
+                               }
+                       } else {
+                               bool empty = true;
+                               foreach (var element in source) {
+                                       if (empty){
+                                               max = element;
+                                               empty = false;
+                                               continue;
+                                       }
+                                       if (comparer.Compare (element, max) > 0)
+                                               max = element;
+                               }
+                               if (empty)
+                                       throw EmptySequence ();
+                       }
+                       return max;
                }
 
                public static int Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, int.MinValue, (a, b) => Math.Max (selector (a), b));
+                       bool empty = true;
+                       var max = int.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (selector (element), max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return max;
                }
 
                public static long Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, long.MinValue, (a, b) => Math.Max (selector (a), b));
+                       bool empty = true;
+                       var max = long.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (selector (element), max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return max;
                }
 
                public static double Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, double.MinValue, (a, b) => Math.Max (selector (a), b));
+                       bool empty = true;
+                       var max = double.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (selector (element), max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return max;
                }
 
                public static float Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, float.MinValue, (a, b) => Math.Max (selector (a), b));
+                       bool empty = true;
+                       var max = float.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (selector (element), max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return max;
                }
 
                public static decimal Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, decimal.MinValue, (a, b) => Math.Max (selector (a), b));
+                       bool empty = true;
+                       var max = decimal.MinValue;
+                       foreach (var element in source){
+                               max = Math.Max (selector (element), max);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return max;
                }
 
                static U Iterate<T, U> (IEnumerable<T> source, U initValue, Func<T, U, U> selector)
@@ -1232,7 +1628,7 @@ namespace System.Linq
                        }
 
                        if (empty)
-                               throw new InvalidOperationException ();
+                               throw NoMatchingElement ();
 
                        return initValue;
                }
@@ -1241,41 +1637,112 @@ namespace System.Linq
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a > b);
+                       bool empty = true;
+                       int? max = null;
+                       foreach (var element in source) {
+                               int? item = selector (element);
+
+                               if (!max.HasValue)
+                                       max = item;
+                               else if (item > max)
+                                       max = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return max;
                }
 
                public static long? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a > b);
+                       bool empty = true;
+                       long? max = null;
+                       foreach (var element in source) {
+                               long? item = selector (element);
+
+                               if (!max.HasValue)
+                                       max = item;
+                               else if (item > max)
+                                       max = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return max;
                }
 
                public static double? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a > b);
+                       bool empty = true;
+                       double? max = null;
+                       foreach (var element in source) {
+                               double? item = selector (element);
+
+                               if (!max.HasValue)
+                                       max = item;
+                               else if (item > max)
+                                       max = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return max;
                }
 
                public static float? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a > b);
+                       bool empty = true;
+                       float? max = null;
+                       foreach (var element in source) {
+                               float? item = selector (element);
+
+                               if (!max.HasValue)
+                                       max = item;
+                               else if (item > max)
+                                       max = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return max;
                }
 
                public static decimal? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a > b);
+                       bool empty = true;
+                       decimal? max = null;
+                       foreach (var element in source) {
+                               decimal? item = selector (element);
+
+                               if (!max.HasValue)
+                                       max = item;
+                               else if (item > max)
+                                       max = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return max;
                }
 
                public static TResult Max<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
+                       // TODO: inline
                        return source.Select (selector).Max ();
                }
 
@@ -1287,70 +1754,180 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       return Iterate (source, int.MaxValue, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = int.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (element, min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return min;
                }
 
                public static long Min (this IEnumerable<long> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, long.MaxValue, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = long.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (element, min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return min;
                }
 
                public static double Min (this IEnumerable<double> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, double.MaxValue, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = double.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (element, min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return min;
                }
 
                public static float Min (this IEnumerable<float> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, float.MaxValue, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = float.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (element, min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return min;
                }
 
                public static decimal Min (this IEnumerable<decimal> source)
                {
                        Check.Source (source);
 
-                       return Iterate (source, decimal.MaxValue, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = decimal.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (element, min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw EmptySequence ();
+                       return min;
                }
 
                public static int? Min (this IEnumerable<int?> source)
                {
                        Check.Source (source);
 
-                       return IterateNullable (source, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = int.MaxValue;
+                               
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               min = Math.Min (element.Value, min);
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+
+                       return min;
                }
 
                public static long? Min (this IEnumerable<long?> source)
                {
                        Check.Source (source);
 
-                       return IterateNullable (source, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = long.MaxValue;
+                               
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               min = Math.Min (element.Value, min);
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+
+                       return min;
                }
 
                public static double? Min (this IEnumerable<double?> source)
                {
                        Check.Source (source);
 
-                       return IterateNullable (source, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = double.MaxValue;
+                               
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               min = Math.Min (element.Value, min);
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+
+                       return min;
                }
 
                public static float? Min (this IEnumerable<float?> source)
                {
                        Check.Source (source);
 
-                       return IterateNullable (source, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = float.MaxValue;
+                               
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               min = Math.Min (element.Value, min);
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+
+                       return min;
                }
 
                public static decimal? Min (this IEnumerable<decimal?> source)
                {
                        Check.Source (source);
 
-                       return IterateNullable (source, (a, b) => Math.Min (a, b));
+                       bool empty = true;
+                       var min = decimal.MaxValue;
+                               
+                       foreach (var element in source) {
+                               if (!element.HasValue)
+                                       continue;
+
+                               min = Math.Min (element.Value, min);
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+
+                       return min;
                }
 
                public static TSource Min<TSource> (this IEnumerable<TSource> source)
@@ -1358,88 +1935,219 @@ namespace System.Linq
                        Check.Source (source);
 
                        var comparer = Comparer<TSource>.Default;
-                       Func<TSource, TSource, bool> compare = (a, b) => comparer.Compare (a, b) < 0;
 
-                       if (default (TSource) == null)
-                               return IterateNullable (source, compare);
+                       TSource min = default (TSource);
+                       
+                       if (default (TSource) == null){
+                               foreach (var element in source) {
+                                       if (element == null)
+                                               continue;
 
-                       return IterateNonNullable (source, compare);
+                                       if (min == null || comparer.Compare (element, min) < 0)
+                                               min = element;
+                               }
+                       } else {
+                               bool empty = true;
+                               foreach (var element in source) {
+                                       if (empty){
+                                               min = element;
+                                               empty = false;
+                                               continue;
+                                       }
+                                       if (comparer.Compare (element, min) < 0)
+                                               min = element;
+                               }
+                               if (empty)
+                                       throw EmptySequence ();
+                       }
+                       return min;
                }
 
                public static int Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, int.MaxValue, (a, b) => Math.Min (selector (a), b));
+                       bool empty = true;
+                       var min = int.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (selector (element), min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return min;
                }
 
                public static long Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, long.MaxValue, (a, b) => Math.Min (selector (a), b));
+                       bool empty = true;
+                       var min = long.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (selector (element), min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return min;
                }
 
                public static double Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, double.MaxValue, (a, b) => Math.Min (selector (a), b));
+                       bool empty = true;
+                       var min = double.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (selector (element), min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return min;
                }
 
                public static float Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, float.MaxValue, (a, b) => Math.Min (selector (a), b));
+                       bool empty = true;
+                       var min = float.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (selector (element), min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return min;
                }
 
                public static decimal Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Iterate (source, decimal.MaxValue, (a, b) => Math.Min (selector (a), b));
+                       bool empty = true;
+                       var min = decimal.MaxValue;
+                       foreach (var element in source){
+                               min = Math.Min (selector (element), min);
+                               empty = false;
+                       }
+                       if (empty)
+                               throw NoMatchingElement ();
+                       return min;
                }
 
                public static int? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a < b);
+                       bool empty = true;
+                       int? min = null;
+                       foreach (var element in source) {
+                               int? item = selector (element);
+
+                               if (!min.HasValue)
+                                       min = item;
+                               else if (item < min)
+                                       min = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return min;
                }
 
                public static long? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a < b);
+                       bool empty = true;
+                       long? min = null;
+                       foreach (var element in source) {
+                               long? item = selector (element);
+
+                               if (!min.HasValue)
+                                       min = item;
+                               else if (item < min)
+                                       min = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return min;
                }
 
                public static float? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a < b);
+                       bool empty = true;
+                       float? min = null;
+                       foreach (var element in source) {
+                               float? item = selector (element);
+
+                               if (!min.HasValue)
+                                       min = item;
+                               else if (item < min)
+                                       min = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return min;
                }
 
                public static double? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a < b);
+                       bool empty = true;
+                       double? min = null;
+                       foreach (var element in source) {
+                               double? item = selector (element);
+
+                               if (!min.HasValue)
+                                       min = item;
+                               else if (item < min)
+                                       min = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return min;
                }
 
                public static decimal? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return IterateNullable (source, selector, (a, b) => a < b);
+                       bool empty = true;
+                       decimal? min = null;
+                       foreach (var element in source) {
+                               decimal? item = selector (element);
+
+                               if (!min.HasValue)
+                                       min = item;
+                               else if (item < min)
+                                       min = item;
+                               empty = false;
+                       }
+
+                       if (empty)
+                               return null;
+                       return min;
                }
 
                public static TResult Min<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
+                       // TODO: inline
                        return source.Select (selector).Min ();
                }
 
@@ -1507,18 +2215,16 @@ namespace System.Linq
                        if (count < 0)
                                throw new ArgumentOutOfRangeException ("count");
 
-                       long upto = ((long) start + count) - 1;
-
-                       if (upto > int.MaxValue)
+                       if (((long) start + count) - 1L > int.MaxValue)
                                throw new ArgumentOutOfRangeException ();
 
-                       return CreateRangeIterator (start, (int) upto);
+                       return CreateRangeIterator (start, count);
                }
 
-               static IEnumerable<int> CreateRangeIterator (int start, int upto)
+               static IEnumerable<int> CreateRangeIterator (int start, int count)
                {
-                       for (int i = start; i <= upto; i++)
-                               yield return i;
+                       for (int i = 0; i < count; i++)
+                               yield return start + i;
                }
 
                #endregion
@@ -1547,17 +2253,15 @@ namespace System.Linq
                {
                        Check.Source (source);
 
-                       var list = source as IList<TSource>;
-                       if (list == null)
-                               list = new List<TSource> (source);
-
-                       return CreateReverseIterator (list);
+                       return CreateReverseIterator (source);
                }
 
-               static IEnumerable<TSource> CreateReverseIterator<TSource> (IList<TSource> source)
+               static IEnumerable<TSource> CreateReverseIterator<TSource> (IEnumerable<TSource> source)
                {
-                       for (int i = source.Count; i > 0; --i)
-                               yield return source [i - 1];
+                       var array = source.ToArray ();
+
+                       for (int i = array.Length - 1; i >= 0; i--)
+                               yield return array [i];
                }
 
                #endregion
@@ -1629,11 +2333,11 @@ namespace System.Linq
                }
 
                public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
-                       Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
+                       Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
                {
-                       Check.SourceAndCollectionSelectors (source, collectionSelector, selector);
+                       Check.SourceAndCollectionSelectors (source, collectionSelector, resultSelector);
 
-                       return CreateSelectManyIterator (source, collectionSelector, selector);
+                       return CreateSelectManyIterator (source, collectionSelector, resultSelector);
                }
 
                static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
@@ -1645,11 +2349,11 @@ namespace System.Linq
                }
 
                public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
-                       Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
+                       Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
                {
-                       Check.SourceAndCollectionSelectors (source, collectionSelector, selector);
+                       Check.SourceAndCollectionSelectors (source, collectionSelector, resultSelector);
 
-                       return CreateSelectManyIterator (source, collectionSelector, selector);
+                       return CreateSelectManyIterator (source, collectionSelector, resultSelector);
                }
 
                static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
@@ -1675,14 +2379,14 @@ namespace System.Linq
                                        continue;
 
                                if (found)
-                                       throw new InvalidOperationException ();
+                                       throw MoreThanOneMatchingElement ();
 
                                found = true;
                                item = element;
                        }
 
                        if (!found && fallback == Fallback.Throw)
-                               throw new InvalidOperationException ();
+                               throw NoMatchingElement ();
 
                        return item;
                }
@@ -1691,8 +2395,26 @@ namespace System.Linq
                {
                        Check.Source (source);
 
+#if !FULL_AOT_RUNTIME
                        return source.Single (PredicateOf<TSource>.Always, Fallback.Throw);
-               }
+#else
+                       var found = false;
+                       var item = default (TSource);
+
+                       foreach (var element in source) {
+                               if (found)
+                                       throw MoreThanOneElement ();
+
+                               found = true;
+                               item = element;
+                       }
+
+                       if (!found)
+                               throw NoMatchingElement ();
+
+                       return item;
+#endif
+        }
 
                public static TSource Single<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
                {
@@ -1709,8 +2431,23 @@ namespace System.Linq
                {
                        Check.Source (source);
 
+#if !FULL_AOT_RUNTIME
                        return source.Single (PredicateOf<TSource>.Always, Fallback.Default);
-               }
+#else
+                       var found = false;
+                       var item = default (TSource);
+
+                       foreach (var element in source) {
+                               if (found)
+                                       throw MoreThanOneMatchingElement ();
+
+                               found = true;
+                               item = element;
+                       }
+
+                       return item;
+#endif
+        }
 
                public static TSource SingleOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
                {
@@ -1803,175 +2540,231 @@ namespace System.Linq
                public static int Sum (this IEnumerable<int> source)
                {
                        Check.Source (source);
-
-                       return Sum<int, int> (source, (a, b) => checked (a + b));
+                       int total = 0;
+                       
+                       foreach (var element in source)
+                               total = checked (total + element);
+                       return total;
                }
 
                public static int? Sum (this IEnumerable<int?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<int?, int?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
+                       int total = 0;
+                       foreach (var element in source) {
+                               if (element.HasValue)
+                                       total = checked (total + element.Value);
+                       }
+                       return total;
                }
 
                public static int Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
                {
                        Check.SourceAndSelector (source, selector);
+                       int total = 0;
+
+                       foreach (var element in source)
+                               total = checked (total + selector (element));
 
-                       return Sum<TSource, int> (source, (a, b) => checked (a + selector (b)));
+                       return total;
                }
 
                public static int? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.SumNullable<TSource, int?> (0, (a, b) => {
-                               var value = selector (b);
-                               return value.HasValue ? checked (a + value.Value) : a;
-                       });
+                       int total = 0;
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (value.HasValue)
+                                       total = checked (total + value.Value);
+                       }
+                       return total;
                }
 
                public static long Sum (this IEnumerable<long> source)
                {
                        Check.Source (source);
 
-                       return Sum<long, long> (source, (a, b) => checked (a + b));
+                       long total = 0;
+                       
+                       foreach (var element in source)
+                               total = checked (total + element);
+                       return total;
                }
 
                public static long? Sum (this IEnumerable<long?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<long?, long?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
+                       long total = 0;
+                       foreach (var element in source) {
+                               if (element.HasValue)
+                                       total = checked (total + element.Value);
+                       }
+                       return total;
                }
 
                public static long Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Sum<TSource, long> (source, (a, b) => checked (a + selector (b)));
+                       long total = 0;
+                       foreach (var element in source)
+                               total = checked (total + selector (element));
+                       return total;
                }
 
                public static long? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.SumNullable<TSource, long?> (0, (a, b) => {
-                               var value = selector (b);
-                               return value.HasValue ? checked (a + value.Value) : a;
-                       });
+                       long total = 0;
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (value.HasValue)
+                                       total = checked (total + value.Value);
+                       }
+                       return total;
                }
 
                public static double Sum (this IEnumerable<double> source)
                {
                        Check.Source (source);
 
-                       return Sum<double, double> (source, (a, b) => checked (a + b));
+                       double total = 0;
+                       
+                       foreach (var element in source)
+                               total = checked (total + element);
+                       return total;
                }
 
                public static double? Sum (this IEnumerable<double?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<double?, double?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
+                       double total = 0;
+                       foreach (var element in source) {
+                               if (element.HasValue)
+                                       total = checked (total + element.Value);
+                       }
+                       return total;
                }
 
                public static double Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return Sum<TSource, double> (source, (a, b) => checked (a + selector (b)));
+                       double total = 0;
+                       
+                       foreach (var element in source)
+                               total = checked (total + selector (element));
+                       return total;
                }
 
                public static double? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.SumNullable<TSource, double?> (0, (a, b) => {
-                               var value = selector (b);
-                               return value.HasValue ? checked (a + value.Value) : a;
-                       });
+                       double total = 0;
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (value.HasValue)
+                                       total = checked (total + value.Value);
+                       }
+                       return total;
                }
 
                public static float Sum (this IEnumerable<float> source)
                {
                        Check.Source (source);
 
-                       return Sum<float, float> (source, (a, b) => checked (a + b));
+                       float total = 0;
+                       
+                       foreach (var element in source)
+                               total = checked (total + element);
+                       return total;
                }
 
                public static float? Sum (this IEnumerable<float?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<float?, float?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
+                       float total = 0;
+                       foreach (var element in source) {
+                               if (element.HasValue)
+                                       total = checked (total + element.Value);
+                       }
+                       return total;
+
                }
 
                public static float Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
                {
                        Check.SourceAndSelector (source, selector);
-
-                       return Sum<TSource, float> (source, (a, b) => checked (a + selector (b)));
+                       float total = 0;
+                       foreach (var element in source)
+                               total = checked (total + selector (element));
+                       return total;
                }
 
                public static float? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.SumNullable<TSource, float?> (0, (a, b) => {
-                               var value = selector (b);
-                               return value.HasValue ? checked (a + value.Value) : a;
-                       });
+                       float total = 0;
+                       foreach (var element in source) {
+                               var value = selector (element);
+                               if (value.HasValue)
+                                       total = checked (total + value.Value);
+                       }
+                       return total;
                }
 
                public static decimal Sum (this IEnumerable<decimal> source)
                {
                        Check.Source (source);
-
-                       return Sum<decimal, decimal> (source, (a, b) => checked (a + b));
+                       decimal total = 0;
+                       
+                       foreach (var element in source)
+                               total = checked (total + element);
+                       return total;
                }
 
                public static decimal? Sum (this IEnumerable<decimal?> source)
                {
                        Check.Source (source);
 
-                       return source.SumNullable<decimal?, decimal?> (0, (total, element) => element.HasValue ? checked (total + element) : total);
+                       decimal total = 0;
+                       foreach (var element in source) {
+                               if (element.HasValue)
+                                       total = checked (total + element.Value);
+                       }
+                       return total;
+
                }
 
                public static decimal Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
                {
                        Check.SourceAndSelector (source, selector);
-
-                       return Sum<TSource, decimal> (source, (a, b) => checked (a + selector (b)));
+                       decimal total = 0;
+                       
+                       foreach (var element in source)
+                               total = checked (total + selector (element));
+                       return total;
                }
 
                public static decimal? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
                {
                        Check.SourceAndSelector (source, selector);
 
-                       return source.SumNullable<TSource, decimal?> (0, (a, b) => {
-                               var value = selector (b);
-                               return value.HasValue ? checked (a + value.Value) : a;
-                       });
-               }
-
-               static TR Sum<TA, TR> (this IEnumerable<TA> source, Func<TR, TA, TR> selector)
-               {
-                       TR total = default (TR);
+                       decimal total = 0;
                        foreach (var element in source) {
-                               total = selector (total, element);
+                               var value = selector (element);
+                               if (value.HasValue)
+                                       total = checked (total + value.Value);
                        }
-
-                       return total;
-               }
-
-               static TR SumNullable<TA, TR> (this IEnumerable<TA> source, TR zero, Func<TR, TA, TR> selector)
-               {
-                       TR total = zero;
-                       foreach (var element in source) {
-                               total = selector (total, element);
-                       }
-
                        return total;
                }
 
@@ -2054,6 +2847,12 @@ namespace System.Linq
                {
                        Check.SourceAndKeySelector (source, keySelector);
 
+#if FULL_AOT_RUNTIME
+                       var oe = source as OrderedEnumerable <TSource>;
+                       if (oe != null)
+                               return oe.CreateOrderedEnumerable (keySelector, comparer, false);
+#endif
+
                        return source.CreateOrderedEnumerable (keySelector, comparer, false);
                }
 
@@ -2072,6 +2871,11 @@ namespace System.Linq
                {
                        Check.SourceAndKeySelector (source, keySelector);
 
+#if FULL_AOT_RUNTIME
+                       var oe = source as OrderedEnumerable <TSource>;
+                       if (oe != null)
+                               return oe.CreateOrderedEnumerable (keySelector, comparer, true);
+#endif
                        return source.CreateOrderedEnumerable (keySelector, comparer, true);
                }
 
@@ -2083,14 +2887,34 @@ namespace System.Linq
                {
                        Check.Source (source);
 
+                       TSource[] array;
                        var collection = source as ICollection<TSource>;
                        if (collection != null) {
-                               var array = new TSource [collection.Count];
+                               if (collection.Count == 0)
+                                       return EmptyOf<TSource>.Instance;
+                               
+                               array = new TSource [collection.Count];
                                collection.CopyTo (array, 0);
                                return array;
                        }
 
-                       return new List<TSource> (source).ToArray ();
+                       int pos = 0;
+                       array = EmptyOf<TSource>.Instance;
+                       foreach (var element in source) {
+                               if (pos == array.Length) {
+                                       if (pos == 0)
+                                               array = new TSource [4];
+                                       else
+                                               Array.Resize (ref array, pos * 2);
+                               }
+
+                               array[pos++] = element;
+                       }
+
+                       if (pos != array.Length)
+                               Array.Resize (ref array, pos);
+                       
+                       return array;
                }
 
                #endregion
@@ -2150,7 +2974,7 @@ namespace System.Linq
                public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source,
                        Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
                {
-                       return ToLookup<TSource, TKey, TSource> (source, keySelector, element => element, comparer);
+                       return ToLookup<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, comparer);
                }
 
                public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
@@ -2251,7 +3075,7 @@ namespace System.Linq
                        }
 
                        foreach (var element in second) {
-                               if (! items.Contains (element, comparer)) {
+                               if (! items.Contains (element)) {
                                        items.Add (element);
                                        yield return element;
                                }
@@ -2259,6 +3083,33 @@ namespace System.Linq
                }
 
                #endregion
+               
+#if NET_4_0
+               #region Zip
+               
+               public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult> (this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
+               {
+                       Check.FirstAndSecond (first, second);
+                       if (resultSelector == null)
+                               throw new ArgumentNullException ("resultSelector");
+                               
+                       return CreateZipIterator (first, second, resultSelector);
+               }
+               
+               static IEnumerable<TResult> CreateZipIterator<TFirst, TSecond, TResult> (IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
+               {
+                       using (IEnumerator<TFirst> first_enumerator = first.GetEnumerator ()) {
+                               using (IEnumerator<TSecond> second_enumerator = second.GetEnumerator ()) {
+
+                                       while (first_enumerator.MoveNext () && second_enumerator.MoveNext ()) {
+                                               yield return selector (first_enumerator.Current, second_enumerator.Current);
+                                       }
+                               }
+                       }
+               }
+               
+               #endregion
+#endif         
 
                #region Where
 
@@ -2266,6 +3117,11 @@ namespace System.Linq
                {
                        Check.SourceAndPredicate (source, predicate);
 
+                       // It cannot be IList<TSource> because it may break on user implementation
+                       var array = source as TSource[];
+                       if (array != null)
+                               return CreateWhereIterator (array, predicate);
+
                        return CreateWhereIterator (source, predicate);
                }
 
@@ -2276,14 +3132,27 @@ namespace System.Linq
                                        yield return element;
                }
 
+               static IEnumerable<TSource> CreateWhereIterator<TSource> (TSource[] source, Func<TSource, bool> predicate)
+               {
+                       for (int i = 0; i < source.Length; ++i) {
+                               var element = source [i];
+                               if (predicate (element))
+                                       yield return element;
+                       }
+               }       
+
                public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
                {
                        Check.SourceAndPredicate (source, predicate);
 
+                       var array = source as TSource[];
+                       if (array != null)
+                               return CreateWhereIterator (array, predicate);
+
                        return CreateWhereIterator (source, predicate);
                }
 
-               static IEnumerable<TSource> CreateWhereIterator<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
+               static IEnumerable<TSource> CreateWhereIterator<TSource> (IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
                {
                        int counter = 0;
                        foreach (TSource element in source) {
@@ -2293,6 +3162,15 @@ namespace System.Linq
                        }
                }
 
+               static IEnumerable<TSource> CreateWhereIterator<TSource> (TSource[] source, Func<TSource, int, bool> predicate)
+               {
+                       for (int i = 0; i < source.Length; ++i) {
+                               var element = source [i];
+                               if (predicate (element, i))
+                                       yield return element;
+                       }
+               }
+
                #endregion
 
                internal static ReadOnlyCollection<TSource> ToReadOnlyCollection<TSource> (this IEnumerable<TSource> source)
@@ -2306,5 +3184,26 @@ namespace System.Linq
 
                        return new ReadOnlyCollection<TSource> (source.ToArray<TSource> ());
                }
+
+               #region Exception helpers
+
+               static Exception EmptySequence ()
+               {
+                       return new InvalidOperationException (Locale.GetText ("Sequence contains no elements"));
+               }
+               static Exception NoMatchingElement ()
+               {
+                       return new InvalidOperationException (Locale.GetText ("Sequence contains no matching element"));
+               }
+               static Exception MoreThanOneElement ()
+               {
+                       return new InvalidOperationException (Locale.GetText ("Sequence contains more than one element"));
+               }
+               static Exception MoreThanOneMatchingElement ()
+               {
+                       return new InvalidOperationException (Locale.GetText ("Sequence contains more than one matching element"));
+               }
+
+               #endregion
        }
 }