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>, IReadOnlyCollection<T>, and ICollection<T>, beyond what LINQ provides. More...


Source files:

Remarks

Additional extension methods for IEnumerable<T>, IReadOnlyCollection<T>, and ICollection<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 BufferedSequence< T > Buffered< T > (this IEnumerator< T > source)
 
static BufferedSequence< T > Buffered< T > (this IEnumerable< T > source)
 
static IListSource< T > Buffered< T > (this IListSource< T > source)
 
static IReadOnlyCollection< T > AsReadOnly< T > (this ICollection< T > c)
 Treats any ICollection{T} object to IReadOnlyCollection{T}. More...
 
static IReadOnlyDictionary< K, V > AsReadOnlyDictionary< K, V > (this IReadOnlyCollection< K > keys, Func< K, Maybe< V >> tryGetValue, Func< K, V > getValue=null)
 Converts a collection of keys to an IReadOnlyDictionary, based on a function that can obtain a value for a given key. More...
 
static void ForEach< T > (this IEnumerable< T > list, Action< T > action)
 
static IEnumerable< KeyValuePair< int, T > > WithIndexes< T > (this IEnumerable< T > c)
 
static ? int FirstIndexWhere< T > (this IEnumerable< T > list, Func< T, bool > pred)
 Gets the lowest index at which a condition is true, or null if nowhere. More...
 
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)
 

Member Function Documentation

◆ AdjacentPairs< T >()

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)})

◆ AdjacentPairsCircular< T >()

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)}.)

◆ AsReadOnly< T >()

static IReadOnlyCollection<T> Loyc.Collections.EnumerableExt.AsReadOnly< T > ( this ICollection< T >  c)
inlinestatic

Treats any ICollection{T} object to IReadOnlyCollection{T}.

This method is named "AsReadOnly" and not "ToReadOnly" because, in contrast to methods like ToArray(), and ToList() it does not make a copy of the sequence, although it does create a new wrapper object.

◆ AsReadOnlyDictionary< K, V >()

static IReadOnlyDictionary<K, V> Loyc.Collections.EnumerableExt.AsReadOnlyDictionary< K, V > ( this IReadOnlyCollection< K >  keys,
Func< K, Maybe< V >>  tryGetValue,
Func< K, V >  getValue = null 
)
inlinestatic

Converts a collection of keys to an IReadOnlyDictionary, based on a function that can obtain a value for a given key.

Parameters
keysA collection of dictionary keys.
tryGetValueThis function is used both to test membership and to get values.
getValueThis function is optional. It is used to get values when it is known in advance that the key exists (in GetEnumerator() and in the Values property). If this is null, tryGetValue is used instead. Providing this function can increase performance.

This function is useful, for example, when you need to implement an interface that provides a dictionary of values, but your data is in the wrong format. You don't want to convert the entire dictionary, since the caller might only need to look up one item from it.

interface ICompany
{
IReadOnlyDictionary<long, string> Employees { get; }
...
}
class Company : ICompany
{
Dictionary<int, Person> _employees = new Dictionary<int, Person>();
public IReadOnlyDictionary<long, string> Employees =>
LinqToLists.Select(_employees.Keys, k => (long)k)
.AsReadOnlyDictionary(k => {
var v = _employees.TryGetValue((int)k);
return v.HasValue ? (Maybe<string>)v.Value.ToString() : Maybe<string>.NoValue;
});
<br>
...
}
class Person
{
string FirstName, LastName;
public override string ToString() => FirstName + " " + LastName;
}

◆ FirstIndexWhere< T >()

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

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

◆ IndexOf< T >()

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.

◆ IndexWhere< T >()

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

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

◆ MaxOrDefault< T >()

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.

◆ MinOrDefault< T >()

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.

◆ SelectFilter< T, Out >()

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.

◆ SequenceHashCode< T >()

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.

◆ Upcast< Base, Derived >()

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