Enhanced C#
Language of your choice: library documentation
Static Public Member Functions | List of all members
Loyc.Collections.EnumerableExt Class Reference

Additional extension methods for IEnumerable<T>, beyond what LINQ provides. More...


Source file:

Remarks

Additional extension methods for IEnumerable<T>, beyond what LINQ provides.

The methods include WithIndexes<T>, which pairs each item of a sequence with a 0-based index of that item; ForEach<T>, which runs a lambda for each member of a sequence; IndexWhere<T>, which finds the index where a predicate is true; AdjacentPairs<T>, which pairs each list item with the next one, and MinOrDefault, which finds the item such that some associated value is minimized (in contrast to LINQ's Min(), which just returns the minimum value itself.) And there's more.

Static Public Member Functions

static void ForEach< T > (this IEnumerable< T > list, Action< T > action)
 
static IEnumerable< KeyValuePair< int, T > > WithIndexes< T > (this IEnumerable< T > c)
 
static int IndexWhere< T > (this IEnumerable< T > list, Func< T, bool > pred)
 Gets the lowest index at which a condition is true, or -1 if nowhere. More...
 
static int IndexOfMin (this IEnumerable< int > source)
 
static int IndexOfMin< T > (this IEnumerable< T > source, Func< T, int > selector)
 
static int IndexOfMin< T > (this IEnumerable< T > source, Func< T, int > selector, out int min)
 
static int IndexOfMax (this IEnumerable< int > source)
 
static int IndexOfMax< T > (this IEnumerable< T > source, Func< T, int > selector)
 
static int IndexOfMax< T > (this IEnumerable< T > source, Func< T, int > selector, out int max)
 
static int IndexOfMin< T > (this IEnumerable< T > source)
 
static int IndexOfMin< T, R > (this IEnumerable< T > source, Func< T, R > selector)
 
static int IndexOfMin< T, R > (this IEnumerable< T > source, Func< T, R > selector, out R min)
 
static int IndexOfMax< T > (this IEnumerable< T > source)
 
static int IndexOfMax< T, R > (this IEnumerable< T > source, Func< T, R > selector)
 
static int IndexOfMax< T, R > (this IEnumerable< T > source, Func< T, R > selector, out R max)
 
static T MinOrDefault< T > (this IEnumerable< T > list, Func< T, int > selector, T defaultValue=default(T))
 Returns the item in the list that has the minimum value for some selector. More...
 
static T MinOrDefault< T > (this IEnumerable< T > list, Func< T, double > selector, T defaultValue=default(T))
 
static IEnumerable< T > WhereNotNull< T > (this IEnumerable< T > list)
 
static IEnumerable< T > WhereNotNull< T > (this IEnumerable< T?> list)
 
static IEnumerable< Out > SelectFilter< T, Out > (this IEnumerable< T > list, Func< T, Maybe< Out >> filter)
 Combines 'Select' and 'Where' in a single operation. More...
 
static T MaxOrDefault< T > (this IEnumerable< T > list, Func< T, int > selector, T defaultValue=default(T))
 Returns the item in the list that has the maximum value for some selector. More...
 
static T MaxOrDefault< T > (this IEnumerable< T > list, Func< T, double > selector, T defaultValue=default(T))
 
static int IndexOf< T > (this IEnumerable< T > list, T item)
 Determines the index of a specific value. More...
 
static int IndexOf< T > (this IEnumerable< T > list, T item, IEqualityComparer< T > comp)
 
static int SequenceHashCode< T > (this IEnumerable< T > list)
 A companion to Enumerable.SequenceEqual<T> that computes a hashcode for a list. More...
 
static int SequenceHashCode< T > (this IEnumerable< T > list, IEqualityComparer< T > comp)
 
static IEnumerable< Base > Upcast< Base, Derived > (this IEnumerable< Derived > list)
 Upcasts a sequence. More...
 
static IEnumerable< Pair< T, T > > AdjacentPairs< T > (this IEnumerable< T > list)
 Returns all adjacent pairs (e.g. for the list {1,2,3}, returns {(1,2),(2,3)}) More...
 
static IEnumerable< Pair< T, T > > AdjacentPairs< T > (this IEnumerator< T > e)
 
static IEnumerable< Pair< T, T > > AdjacentPairsCircular< T > (this IEnumerable< T > list)
 Returns all adjacent pairs, treating the first and last pairs as adjacent (e.g. for the list {1,2,3,4}, returns the pairs {(1,2),(2,3),(3,4),(4,1)}.) More...
 
static IEnumerable< Pair< T, T > > AdjacentPairsCircular< T > (this IEnumerator< T > e)
 
static List< T > ToList< T > (this IEnumerator< T > e)
 
static DList< T > ToDList< T > (this IReadOnlyCollection< T > c)
 
static DList< T > ToDList< T > (this IList< T > c)
 
static DList< T > ToDList< T > (this IListAndListSource< T > c)
 
static DList< T > ToDList< T > (this IEnumerable< T > c)
 
static DList< T > ToDList< T > (this IEnumerator< T > e, int initialCapacity=4)
 

Member Function Documentation

static IEnumerable<Pair<T, T> > Loyc.Collections.EnumerableExt.AdjacentPairs< T > ( this IEnumerable< T >  list)
inlinestatic

Returns all adjacent pairs (e.g. for the list {1,2,3}, returns {(1,2),(2,3)})

static IEnumerable<Pair<T, T> > Loyc.Collections.EnumerableExt.AdjacentPairsCircular< T > ( this IEnumerable< T >  list)
inlinestatic

Returns all adjacent pairs, treating the first and last pairs as adjacent (e.g. for the list {1,2,3,4}, returns the pairs {(1,2),(2,3),(3,4),(4,1)}.)

References Loyc.Collections.DList< T >.Add().

static int Loyc.Collections.EnumerableExt.IndexOf< T > ( this IEnumerable< T >  list,
item 
)
inlinestatic

Determines the index of a specific value.

Returns
The index of the value, if found, or -1 if it was not found.

At first, this method was a member of IListSource itself, just in case the source might have some kind of fast lookup logic (e.g. binary search) or custom comparer. However, since the item to find is an "in" argument, it would prevent IListSource from being marked covariant when I upgrade to C# 4.

static int Loyc.Collections.EnumerableExt.IndexWhere< T > ( this IEnumerable< T >  list,
Func< T, bool >  pred 
)
inlinestatic

Gets the lowest index at which a condition is true, or -1 if nowhere.

static T Loyc.Collections.EnumerableExt.MaxOrDefault< T > ( this IEnumerable< T >  list,
Func< T, int >  selector,
defaultValue = default(T) 
)
inlinestatic

Returns the item in the list that has the maximum value for some selector.

Template Parameters
T
Parameters
listA list to search
selectorA function that takes a number from the list
defaultValueA value

Unfortunately, the standard LINQ methods Max(lambda) and Min(lambda) return the minimum or maximum value returned from the lambda function, which is unfortunate because you often want the original value from the list, not the number returned by the lambda. That's a flawed design, because often you want the original T value and not the projected number; if the developer actually wanted the min/max number, he could have just used list.Select(lambda).Max() instead of list.Max(lambda).

So MinOrDefault() and MaxByDefault() are different in two ways: (1) they returns the original T value from the collection, and (2) if the collection is empty, they return a default value.

static T Loyc.Collections.EnumerableExt.MinOrDefault< T > ( this IEnumerable< T >  list,
Func< T, int >  selector,
defaultValue = default(T) 
)
inlinestatic

Returns the item in the list that has the minimum value for some selector.

static IEnumerable<Out> Loyc.Collections.EnumerableExt.SelectFilter< T, Out > ( this IEnumerable< T >  list,
Func< T, Maybe< Out >>  filter 
)
inlinestatic

Combines 'Select' and 'Where' in a single operation.

Parameters
filterIf this function returns Maybe<O>.NoValue then the element is suppressed from the output; otherwise the Maybe<T>.Value is sent to the output.
Returns
A sequence filtered and changed by filter.
static int Loyc.Collections.EnumerableExt.SequenceHashCode< T > ( this IEnumerable< T >  list)
inlinestatic

A companion to Enumerable.SequenceEqual<T> that computes a hashcode for a list.

static IEnumerable<Base> Loyc.Collections.EnumerableExt.Upcast< Base, Derived > ( this IEnumerable< Derived >  list)
inlinestatic

Upcasts a sequence.

In .NET 4+ this is a no-op that just returns list, but in .NET 3.5 that's illegal, so this method creates an adapter.

Type Constraints
Derived :class 
Derived :Base