// 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 ())
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / (double) count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / (double) count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / (double) count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / (double) count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / count;
}
count++;
}
if (count == 0)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return total / count;
}
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);
}
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);
}
return element;
if (fallback == Fallback.Throw)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return default (TSource);
}
}
}
- throw new InvalidOperationException ("The source sequence is empty");
+ throw EmptySequence ();
}
public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
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> ());
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);
}
return item;
if (fallback == Fallback.Throw)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return item;
}
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)
if (!empty)
return item;
- throw new InvalidOperationException ();
+ throw EmptySequence ();
#endif
- }
+ }
public static TSource Last<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return max;
}
max = element;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
}
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return max;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return max;
}
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return initValue;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
return min;
}
min = element;
}
if (empty)
- throw new InvalidOperationException ();
+ throw EmptySequence ();
}
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return min;
}
empty = false;
}
if (empty)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return min;
}
continue;
if (found)
- throw new InvalidOperationException ();
+ throw MoreThanOneMatchingElement ();
found = true;
item = element;
}
if (!found && fallback == Fallback.Throw)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return item;
}
foreach (var element in source) {
if (found)
- throw new InvalidOperationException ();
+ throw MoreThanOneElement ();
found = true;
item = element;
}
if (!found)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return item;
#endif
- }
+ }
public static TSource Single<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (var element in source) {
if (found)
- throw new InvalidOperationException ();
+ throw MoreThanOneMatchingElement ();
found = true;
item = element;
return item;
#endif
- }
+ }
public static TSource SingleOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
collection.CopyTo (array, 0);
return array;
}
-
+
int pos = 0;
array = EmptyOf<TSource>.Instance;
foreach (var element in source) {
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,
#endregion
-#if NET_4_0 || MOONLIGHT || MOBILE
+#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.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);
}
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) {
}
}
+ 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)
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
}
}