static IEnumerable<TResult> CreateCastIterator<TResult> (IEnumerable source)
{
- foreach (object element in source)
- yield return (TResult) element;
+ foreach (TResult element in source)
+ yield return element;
}
#endregion
{
Check.Source (source);
- return source.First (PredicateOf<TSource>.Always, Fallback.Throw);
+ var list = source as IList<TSource>;
+ if (list != null) {
+ if (list.Count != 0)
+ return list [0];
+
+ throw new InvalidOperationException ();
+ } else {
+ using (var enumerator = source.GetEnumerator ()) {
+ if (enumerator.MoveNext ())
+ return enumerator.Current;
+ }
+ }
+
+ throw new InvalidOperationException ();
}
public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
Check.SourceAndKeySelector (source, keySelector);
+ return CreateGroupByIterator (source, keySelector, comparer);
+ }
+
+ 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> ();
int counter = 0;
}
}
-
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
{
if (comparer == null)
comparer = EqualityComparer<TKey>.Default;
+ return CreateGroupJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
+ }
+
+ static IEnumerable<TResult> CreateGroupJoinIterator<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
/*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
foreach (U element in inner)
static T? IterateNullable<T> (IEnumerable<T?> source, T initValue, Func<T?, T?, bool> selector) where T : struct
{
- int counter = 0;
+ bool empty = true;
T? value = initValue;
foreach (var element in source) {
if (!element.HasValue)
if (selector (element.Value, value))
value = element;
- ++counter;
+
+ empty = false;
}
- if (counter == 0)
+ if (empty)
return null;
return value;
static U Iterate<T, U> (IEnumerable<T> source, U initValue, Func<T, U, U> selector)
{
- int counter = 0;
+ bool empty = true;
foreach (var element in source) {
initValue = selector (element, initValue);
- ++counter;
+ empty = false;
}
- if (counter == 0)
+ if (empty)
throw new InvalidOperationException ();
return initValue;
static U? IterateNullable<T, U> (IEnumerable<T> source, U initialValue, Func<T, U?, U?> selector) where U : struct
{
- int counter = 0;
+ bool empty = true;
U? value = initialValue;
foreach (var element in source) {
value = selector (element, value);
if (!value.HasValue)
continue;
- ++counter;
+ empty = false;
}
- if (counter == 0)
+ if (empty)
return null;
return value;
int counter = 0;
foreach (TSource element in source) {
- if (counter++ == count)
- yield break;
-
yield return element;
+
+ if (++counter == count)
+ yield break;
}
}