Class EnumerableEx
- Namespace
- Utils.Collections
- Assembly
- Utils.dll
Extension methods for working with IEnumerable<T> and related collections.
public static class EnumerableEx
- Inheritance
-
EnumerableEx
- Inherited Members
Methods
Flatten<T>(IEnumerable<IEnumerable<T>>)
Flattens a sequence of sequences into a single, concatenated sequence.
public static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> sources)
Parameters
sourcesIEnumerable<IEnumerable<T>>An IEnumerable{IEnumerable{T}} containing sub-sequences to flatten.
Returns
- IEnumerable<T>
A single IEnumerable<T> containing all elements from the sub-sequences in order.
Type Parameters
TThe type of elements in the sub-sequences.
FollowedBy<T>(IEnumerable<T>, params IEnumerable<T>)
Enumerates all elements in enumerable followed by
additional elements.
public static IEnumerable<T> FollowedBy<T>(this IEnumerable<T> enumerable, params IEnumerable<T> elements)
Parameters
enumerableIEnumerable<T>The original sequence.
elementsIEnumerable<T>The elements to append.
Returns
- IEnumerable<T>
A sequence that first yields every element of
enumerable, and then yields every element ofelements.
Type Parameters
TThe type of elements in the sequences.
GetValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Returns an element from a dictionary with the specific key or defaultValue if if doesn't exists
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default)
Parameters
dictionaryIDictionary<TKey, TValue>Dictionary
keyTKeyKey whose value to get
defaultValueTValueDefault value
Returns
- TValue
Value of specific key or default value
Type Parameters
TKeyKey type
TValueValue type
Exceptions
- ArgumentNullException
Raise if dictionary is null
HasAtLeastElements<T>(IEnumerable<T>, int)
Indicates whether the enumerable has at least
count elements.
public static bool HasAtLeastElements<T>(this IEnumerable<T> enumerable, int count)
Parameters
enumerableIEnumerable<T>The sequence to check.
countintThe minimum number of elements required.
Returns
Type Parameters
TThe type of elements in the sequence.
Exceptions
- ArgumentNullException
Thrown if
enumerableis null.
HasManyElements<T>(IEnumerable<T>)
Indicates whether the enumerable contains more than one element.
public static bool HasManyElements<T>(this IEnumerable<T> enumerable)
Parameters
enumerableIEnumerable<T>The sequence to check.
Returns
Type Parameters
TThe type of elements in the sequence.
IsNullOrEmptyCollection<T>(IEnumerable<T>)
Returns true if collection is null or has no elements
public static bool IsNullOrEmptyCollection<T>(this IEnumerable<T> coll)
Parameters
collIEnumerable<T>
Returns
Type Parameters
T
Pack<T>(IEnumerable<T>)
Packs consecutive identical elements from source into
groups, yielding a Pack<T>(IEnumerable<T>) for each group containing the
value and its repetition count.
public static IEnumerable<Pack<T>> Pack<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>The sequence of elements to pack.
Returns
- IEnumerable<Pack<T>>
A sequence of Pack<T>(IEnumerable<T>) structures, each containing the element value and how many times it was repeated consecutively.
Type Parameters
TThe type of elements in the sequence.
Remarks
Uses Default for equality checks.
PrecededBy<T>(IEnumerable<T>, params IEnumerable<T>)
Enumerates the specified elements first,
then enumerates all elements in enumerable.
public static IEnumerable<T> PrecededBy<T>(this IEnumerable<T> enumerable, params IEnumerable<T> elements)
Parameters
enumerableIEnumerable<T>The original sequence.
elementsIEnumerable<T>The elements to prepend.
Returns
- IEnumerable<T>
A sequence that first yields every element of
elements, and then yields every element ofenumerable.
Type Parameters
TThe type of elements in the sequences.
Replace<T>(IEnumerable<T>, T[], T[])
Scans the source for occurrences of the subsequence toReplace,
and replaces them with the subsequence replacement.
A naive, non-overlapping approach is used.
public static IEnumerable<T> Replace<T>(this IEnumerable<T> source, T[] toReplace, T[] replacement) where T : IEquatable<T>
Parameters
sourceIEnumerable<T>The source sequence in which to perform replacements.
toReplaceT[]The subsequence to find and replace.
replacementT[]The subsequence to substitute in place of
toReplace.
Returns
- IEnumerable<T>
A new sequence with the replacements applied where the pattern is matched.
Type Parameters
TThe element type; must implement IEquatable<T> for equality checks.
Remarks
Overlapping matches are not recognized. For example, if toReplace is
[1, 2, 1], and the source is [1, 2, 1, 2, 1], this method
only replaces the first match, ignoring the second (overlapping) match.
If toReplace is empty, no replacement is done and the
original source is returned as-is.
Exceptions
- ArgumentNullException
Thrown if any of the parameters (
source,toReplace, orreplacement) is null.
SkipTake<T>(IEnumerable<T>, int, int)
Returns a slice of source starting at skip and
yielding at most take elements, using a single enumerator.
This is equivalent to Skip(skip).Take(take) but avoids the two LINQ
enumerator allocations that the chained form incurs.
public static IEnumerable<T> SkipTake<T>(this IEnumerable<T> source, int skip, int take)
Parameters
sourceIEnumerable<T>The sequence to slice.
skipintNumber of elements to skip from the start.
takeintMaximum number of elements to return.
Returns
- IEnumerable<T>
The requested slice of
source.
Type Parameters
TThe type of elements in the sequence.
Exceptions
- ArgumentNullException
Thrown if
sourceis null.
Slice<T>(IEnumerable<T>, params IEnumerable<int>)
Slices the source into multiple segments at the
specified cutIndexes.
public static IEnumerable<IEnumerable<T>> Slice<T>(this IEnumerable<T> source, params IEnumerable<int> cutIndexes)
Parameters
sourceIEnumerable<T>The IEnumerable<T> to slice.
cutIndexesIEnumerable<int>The indexes at which the sequence is sliced. They should be sorted in ascending order.
Returns
- IEnumerable<IEnumerable<T>>
A sequence of segments (each itself an IEnumerable<T>), split at each index in
cutIndexes.
Type Parameters
TThe type of elements in the sequence.
Remarks
If cutIndexes is empty, then the entire sequence
is returned as a single segment.
SlideEnumerateBy<T>(IEnumerable<T>, int, int)
Enumerates a sliding window of size windowSize over
the source IEnumerable<T>. Each yielded array shares
elements with the previous one in a sliding fashion.
public static IEnumerable<T[]> SlideEnumerateBy<T>(this IEnumerable<T> source, int windowSize, int skipCount = 0)
Parameters
sourceIEnumerable<T>The sequence to read.
windowSizeintThe size of each sliding window.
skipCountintThe number of elements to skip from the start before beginning.
Returns
- IEnumerable<T[]>
An IEnumerable{Array{T}} where each array represents a window of size
windowSize, except possibly the last one if there are not enough remaining elements.
Type Parameters
TThe element type of the source sequence.
Unpack<T>(IEnumerable<Pack<T>>)
Unpacks a sequence of Pack<T>(IEnumerable<T>) into a flat sequence, repeating each value according to its repetition count.
public static IEnumerable<T> Unpack<T>(this IEnumerable<Pack<T>> source)
Parameters
sourceIEnumerable<Pack<T>>The packed sequence to unpack.
Returns
- IEnumerable<T>
A flat IEnumerable<T> restoring the original sequence before packing.
Type Parameters
TThe type of the elements in the packed sequence.
Zip(bool, params IEnumerable[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<object[]> Zip(bool continueAfterShortestListEnds, params IEnumerable[] enumerations)
Parameters
continueAfterShortestListEndsboolContinue when the shortest list ends
enumerationsIEnumerable[]IEnumerable to be read
Returns
- IEnumerable<object[]>
Array of objects
Zip(bool, params (IEnumerable enumeration, object defaultValue)[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<object[]> Zip(bool continueAfterShortestListEnds, params (IEnumerable enumeration, object defaultValue)[] enumerations)
Parameters
continueAfterShortestListEndsboolContinue when the shortest list ends
enumerations(IEnumerable enumeration, object defaultValue)[]IEnumerable to be read
Returns
- IEnumerable<object[]>
Array of objects
Zip(params IEnumerable[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<object[]> Zip(params IEnumerable[] enumerations)
Parameters
enumerationsIEnumerable[]IEnumerable to be read
Returns
- IEnumerable<object[]>
Array of objects
Zip(params (IEnumerable enumeration, object defaultValue)[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<object[]> Zip(params (IEnumerable enumeration, object defaultValue)[] enumerations)
Parameters
enumerations(IEnumerable enumeration, object defaultValue)[]IEnumerable to be read
Returns
- IEnumerable<object[]>
Array of objects
Zip<TResult>(bool, Func<object[], TResult>, params IEnumerable[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<TResult> Zip<TResult>(bool continueAfterShortestListEnds, Func<object[], TResult> transform, params IEnumerable[] enumerations)
Parameters
continueAfterShortestListEndsboolContinue when the shortest list ends
transformFunc<object[], TResult>Transform function
enumerationsIEnumerable[]IEnumerable to be read
Returns
- IEnumerable<TResult>
Array of objects
Type Parameters
TResult
Zip<TResult>(bool, Func<object[], TResult>, params (IEnumerable enumeration, object defaultValue)[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<TResult> Zip<TResult>(bool continueAfterShortestListEnds, Func<object[], TResult> transform, params (IEnumerable enumeration, object defaultValue)[] enumerations)
Parameters
continueAfterShortestListEndsboolContinue when the shortest list ends
transformFunc<object[], TResult>transformation function of input object to output object
enumerations(IEnumerable enumeration, object defaultValue)[]IEnumerable to be read
Returns
- IEnumerable<TResult>
Array of objects
Type Parameters
TResult
Zip<TResult>(Func<object[], TResult>, params IEnumerable[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<TResult> Zip<TResult>(Func<object[], TResult> transform, params IEnumerable[] enumerations)
Parameters
transformFunc<object[], TResult>enumerationsIEnumerable[]IEnumerable to be read
Returns
- IEnumerable<TResult>
Array of objects
Type Parameters
TResult
Zip<TResult>(Func<object[], TResult>, params (IEnumerable enumeration, object defaultValue)[])
Read several IEnumerable in parallel and returns an array of object of elments from the same indexes
public static IEnumerable<TResult> Zip<TResult>(Func<object[], TResult> transform, params (IEnumerable enumeration, object defaultValue)[] enumerations)
Parameters
transformFunc<object[], TResult>enumerations(IEnumerable enumeration, object defaultValue)[]IEnumerable to be read
Returns
- IEnumerable<TResult>
Array of objects
Type Parameters
TResult
Zip<T1, T2>(IEnumerable<T1>, IEnumerable<T2>, T1, T2, bool)
Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations
public static IEnumerable<(T1 Item1, T2 Item2)> Zip<T1, T2>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, T1 default1 = default, T2 default2 = default, bool continueAfterShortestListEnds = true)
Parameters
enumeration1IEnumerable<T1>Enumeration 1
enumeration2IEnumerable<T2>Enumeration 2
default1T1Default value after
enumeration1has enddefault2T2Default value after
enumeration2has endcontinueAfterShortestListEndsboolContinue to enumerate the longuest enumeration when the shortest has end
Returns
- IEnumerable<(T1 Key, T2 Value)>
Type Parameters
T1Type for
enumeration1elementsT2Type for
enumeration2elements
Zip<T1, T2, T3>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, T1, T2, T3, bool)
Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations
public static IEnumerable<(T1 Item1, T2 Item2, T3 Item3)> Zip<T1, T2, T3>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, IEnumerable<T3> enumeration3, T1 default1 = default, T2 default2 = default, T3 default3 = default, bool continueAfterShortestListEnds = true)
Parameters
enumeration1IEnumerable<T1>Enumeration 1
enumeration2IEnumerable<T2>Enumeration 2
enumeration3IEnumerable<T3>Enumeration 2
default1T1Default value after
enumeration1has enddefault2T2Default value after
enumeration2has enddefault3T3Default value after
enumeration3has endcontinueAfterShortestListEndsboolContinue to enumerate the longuest enumeration when the shortest has end
Returns
- IEnumerable<(T1 Item1, T2 Item2, T3 Item3)>
Type Parameters
T1Type for
enumeration1elementsT2Type for
enumeration2elementsT3Type for
enumeration3elements
Zip<T1, T2, TResult>(IEnumerable<T1>, IEnumerable<T2>, Func<T1, T2, TResult>, bool)
Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations
public static IEnumerable<TResult> Zip<T1, T2, TResult>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, Func<T1, T2, TResult> func, bool continueAfterShortestListEnds = true)
Parameters
enumeration1IEnumerable<T1>Enumeration 1
enumeration2IEnumerable<T2>Enumeration 2
funcFunc<T1, T2, TResult>transformation function
continueAfterShortestListEndsboolContinue to enumerate the longuest enumeration when the shortest has end
Returns
- IEnumerable<TResult>
Type Parameters
T1Type for
enumeration1elementsT2Type for
enumeration2elementsTResultResulting type
Zip<T1, T2, T3, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, Func<T1, T2, T3, TResult>, bool)
Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations
public static IEnumerable<TResult> Zip<T1, T2, T3, TResult>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, IEnumerable<T3> enumeration3, Func<T1, T2, T3, TResult> func, bool continueAfterShortestListEnds = true)
Parameters
enumeration1IEnumerable<T1>Enumeration 1
enumeration2IEnumerable<T2>Enumeration 2
enumeration3IEnumerable<T3>Enumeration 2
funcFunc<T1, T2, T3, TResult>transformation function
continueAfterShortestListEndsboolContinue to enumerate the longuest enumeration when the shortest has end
Returns
- IEnumerable<TResult>
Type Parameters
T1Type for
enumeration1elementsT2Type for
enumeration2elementsT3Type for
enumeration3elementsTResultType for resulting elements