Enhanced C#
Loyc library documentation
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
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)
 Finds the minimum element's index in the list More...
 
static int IndexOfMin< T > (this IEnumerable< T > source, Func< T, int > selector)
 Finds the minimum element's index in the list More...
 
static int IndexOfMin< T > (this IEnumerable< T > source, Func< T, int > selector, [MaybeNull] out T min)
 Finds the minimum element's index in the list, and returns it with the item itself More...
 
static int IndexOfMin< T > (this IEnumerable< T > source)
 Finds the minimum element's index in the list More...
 
static int IndexOfMin< T, R > (this IEnumerable< T > source, Func< T, R > selector)
 Finds the minimum element's index in the list More...
 
static int IndexOfMin< T, R > (this IEnumerable< T > source, Func< T, R > selector, [MaybeNull] out T min)
 Finds the minimum element's index in the list More...
 
static T MinItemOrDefault< T > (this IEnumerable< T > list, Func< T, int > selector, [AllowNull] T defaultValue=default(T))
 Finds the minimum element in the list (given some selector) and returns it More...
 
static T MinItemOrDefault< T, S > (this IEnumerable< T > list, Func< T, S > selector, [AllowNull] T defaultValue=default(T))
 
static T MinOrDefault< T > (this IEnumerable< T > list, Func< T, int > selector, [AllowNull] T defaultValue=default(T))
 
static Maybe< T > MinItem< T > (this IEnumerable< T > list, Func< T, int > selector)
 Finds the minimum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned. More...
 
static Maybe< T > MinItem< T, S > (this IEnumerable< T > list, Func< T, S > selector)
 Finds the minimum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned. More...
 
static int IndexOfMax (this IEnumerable< int > source)
 Finds the maximum element's index in the list More...
 
static int IndexOfMax< T > (this IEnumerable< T > source, Func< T, int > selector)
 Finds the maximum element's index in the list More...
 
static int IndexOfMax< T > (this IEnumerable< T > source, Func< T, int > selector, [MaybeNull] out T min)
 Finds the maximum element's index in the list, and returns it with the item itself More...
 
static int IndexOfMax< T > (this IEnumerable< T > source)
 Finds the maximum element's index in the list More...
 
static int IndexOfMax< T, R > (this IEnumerable< T > source, Func< T, R > selector)
 Finds the maximum element's index in the list More...
 
static int IndexOfMax< T, R > (this IEnumerable< T > source, Func< T, R > selector, [MaybeNull] out T min)
 Finds the maximum element's index in the list More...
 
static T MaxItemOrDefault< T > (this IEnumerable< T > list, Func< T, int > selector, [AllowNull] T defaultValue=default(T))
 Finds the maximum element in the list (given some selector) and returns it More...
 
static T MaxItemOrDefault< T, S > (this IEnumerable< T > list, Func< T, S > selector, [AllowNull] T defaultValue=default(T))
 
static T MaxOrDefault< T > (this IEnumerable< T > list, Func< T, int > selector, [AllowNull] T defaultValue=default(T))
 
static Maybe< T > MaxItem< T > (this IEnumerable< T > list, Func< T, int > selector)
 Finds the maximum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned. More...
 
static Maybe< T > MaxItem< T, S > (this IEnumerable< T > list, Func< T, S > selector)
 Finds the maximum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned. More...
 
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 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

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

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.

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; }); ... } class Person { string FirstName, LastName; public override string ToString() => FirstName + " " + LastName; }

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.

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

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.IndexOfMax ( this IEnumerable< int >  source)
inlinestatic

Finds the maximum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMax< T > ( this IEnumerable< T >  source,
Func< T, int >  selector 
)
static

Finds the maximum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMax< T > ( this IEnumerable< T >  source,
Func< T, int >  selector,
[MaybeNull] out T  min 
)
inlinestatic

Finds the maximum element's index in the list, and returns it with the item itself

static int Loyc.Collections.EnumerableExt.IndexOfMax< T > ( this IEnumerable< T >  source)
inlinestatic

Finds the maximum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMax< T, R > ( this IEnumerable< T >  source,
Func< T, R >  selector 
)
static

Finds the maximum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMax< T, R > ( this IEnumerable< T >  source,
Func< T, R >  selector,
[MaybeNull] out T  min 
)
inlinestatic

Finds the maximum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMin ( this IEnumerable< int >  source)
inlinestatic

Finds the minimum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMin< T > ( this IEnumerable< T >  source,
Func< T, int >  selector 
)
static

Finds the minimum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMin< T > ( this IEnumerable< T >  source,
Func< T, int >  selector,
[MaybeNull] out T  min 
)
inlinestatic

Finds the minimum element's index in the list, and returns it with the item itself

static int Loyc.Collections.EnumerableExt.IndexOfMin< T > ( this IEnumerable< T >  source)
inlinestatic

Finds the minimum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMin< T, R > ( this IEnumerable< T >  source,
Func< T, R >  selector 
)
static

Finds the minimum element's index in the list

static int Loyc.Collections.EnumerableExt.IndexOfMin< T, R > ( this IEnumerable< T >  source,
Func< T, R >  selector,
[MaybeNull] out T  min 
)
inlinestatic

Finds the minimum element's index in the list

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.

static Maybe<T> Loyc.Collections.EnumerableExt.MaxItem< T > ( this IEnumerable< T >  list,
Func< T, int >  selector 
)
inlinestatic

Finds the maximum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned.

static Maybe<T> Loyc.Collections.EnumerableExt.MaxItem< T, S > ( this IEnumerable< T >  list,
Func< T, S >  selector 
)
inlinestatic

Finds the maximum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned.

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

Finds the maximum element in the list (given some selector) and returns it

Parameters
listA list that will be scanned from beginning to end
selectorA function that gets a comparable value for each item
defaultValueA value to return if the list is empty (or all nulls)

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. If the developer actually wanted the min/max number, he could have just used list.Select(lambda).Max().

static Maybe<T> Loyc.Collections.EnumerableExt.MinItem< T > ( this IEnumerable< T >  list,
Func< T, int >  selector 
)
inlinestatic

Finds the minimum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned.

static Maybe<T> Loyc.Collections.EnumerableExt.MinItem< T, S > ( this IEnumerable< T >  list,
Func< T, S >  selector 
)
inlinestatic

Finds the minimum element (as determined by the selector) and returns it. If the list is empty, an empty Maybe{T} value is returned.

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

Finds the minimum element in the list (given some selector) and returns it

Parameters
listA list that will be scanned from beginning to end
selectorA function that gets a comparable value for each item
defaultValueA value to return if the list is empty (or all nulls)

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. If the developer actually wanted the min/max number, he could have just used list.Select(lambda).Max().

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