Enumerable<T> class

The base class for all enumerables. Allows advanced enumeration methods to be performed on the underlying collection.

Enumeration methods are separated into two categories: value methods and lazy-execution methods.

Value methods are methods that condense the enumerable into a single static value. These methods will iterate over the enumerable when the method is called, and may iterate over the entire enumerable (possibly more than once) or may short-circuit the iteration after certain conditions have been met. Examples of value methods include aggregateE, maxE, and countE.

Lazy-execution methods are methods that modify the enumeration using a particular tactic. These methods do not execute until the enumeration is iterated over, which can drastically improve performance when dealing with large collections of data. These methods will themselves return an instance of Enumerable, so they can be chained together to perform multiple transformations at once, further improving performance. Examples of lazy-execution methods include selectE, groupByE, and distinctE.

(Note: as part of their iteration, some deferred-execution methods must first iterate over the entire underlying collection before they can begin their own iteration. Take caution when applying these methods to large collections as they can block the thread while the initial computation takes place. Examples of this are reverseE and orderByE.)

To create an Enumerable out of a List, Set, or any other Iterable, use the Enumerable.from factory method or use the E convenience global function:

final list = [0, 1, 2, 3, 4];

// This works
final first = Enumerable.from(list);

// This also works
final second = E(list);

Enumerable extends Iterable, so any instance of Enumerable can be iterated over using a traditional for loop:

final list = [0, 1, 2, 3, 4];
for (var element in E(list).Select((i) => i * 2)) {
  print(element);
}

// Output:
// 0
// 2
// 4
// 6
// 8

Enumerable objects can be converted back into Dart collections by utilizing the Enumerable.toListE, Enumerable.toMapE, or Enumerable.toSetE methods.

final list = [0, 1, 2, 3, 4];
final result = E(list).Select((i) => i * 2).ToList();
print(result);

// Output:
// [0, 2, 4, 6, 8]
Inheritance
  • Object
  • Iterable<T>
  • Enumerable

Constructors

Enumerable()
Enumerable.empty()
Creates an empty enumerable. [...]
factory
Enumerable.from(Iterable<T> iterable)
Creates an enumerable from an Iterable. [...]
factory
Enumerable.generate(int count, Generator<T> generator)
factory
Enumerable.repeat(T value, int count)
Creates an enumerable [...]
factory

Properties

first → T
Returns the first element. [...]
read-only, inherited
hashCode → int
The hash code for this object. [...]
read-only, inherited
isEmpty → bool
Returns true if there are no elements in this collection. [...]
read-only, inherited
isNotEmpty → bool
Returns true if there is at least one element in this collection. [...]
read-only, inherited
iterator → Iterator<T>
Returns a new Iterator that allows iterating the elements of this Iterable. [...]
read-only, inherited
last → T
Returns the last element. [...]
read-only, inherited
length → int
Returns the number of elements in this. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited
single → T
Checks that this iterable has only one element, and returns that element. [...]
read-only, inherited

Methods

aggregateE([Aggregator<T> aggregator, T initialValue ]) → T
Aggregates the enumerable into a single value. [...]
allE([Condition<T> condition ]) → bool
Returns true if all elements match a condition and false otherwise. [...]
anyE([Condition<T> condition ]) → bool
Returns true if any one element matches a condition and false otherwise. [...]
appendE(T newElement) Enumerable<T>
Inserts an element to the end of the enumerable. [...]
averageE() → T
Calculates the average of all numerical values in the enumerable. [...]
castE<TResult>([CastTransformer<T, TResult> transformer ]) Enumerable<TResult>
Casts each element in the enumerable from one type to another. [...]
concatE(Iterable<T> other) Enumerable<T>
Concatenates this enumerable and another collection. [...]
containsE(T value, { EqualityComparer<T> comparer }) → bool
Returns true if the specified element exists in the enumerable and false otherwise. [...]
countE([Condition<T> condition ]) → int
Returns the number of elements in the enumerable. [...]
defaultIfEmptyE(T value) Enumerable<T>
Returns either this enumerable or a new enumerable containing value if this enumerable is empty. [...]
distinctE({EqualityComparer<T> comparer }) Enumerable<T>
Returns an enumerable representing the distinct values of this enumerable. [...]
elementAtE(int index) → T
Returns the element at the specified index. [...]
elementAtOrDefaultE(int index, { T defaultValue }) → T
Returns the element at the specified index or a default value of one is not found. [...]
exceptE(Iterable<T> other, { EqualityComparer<T> comparer }) Enumerable<T>
Returns the set difference between the enumerable and the given collection. [...]
firstE({Condition<T> condition }) → T
Returns the first element in the enumerable, optionally matching a specified condition. [...]
firstOrDefaultE({Condition<T> condition, T defaultValue }) → T
Returns the first element in the enumerable or a default value if there is none, optionally matching a specified condition. [...]
groupByE<TKey>(Selector<T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) Enumerable<Grouping<TKey, T>>
Groups the elements in the enumerable by a key. [...]
groupByValueE<TKey, TValue>(Selector<T, TKey> keySelector, Selector<T, TValue> valueSelector, { EqualityComparer<TKey> keyComparer }) Enumerable<Grouping<TKey, TValue>>
Groups the elements in the enumerable by a key and maps the elements to a new value. [...]
groupJoinE<TInner, TKey, TResult>(Iterable<TInner> inner, Selector<T, TKey> outerKeySelector, Selector<TInner, TKey> innerKeySelector, GroupSelector<T, Iterable<TInner>, TResult> resultSelector, { EqualityComparer<TKey> keyComparer }) Enumerable<TResult>
Joins elements in the enumerable with a group of all elements in the inner collection that match the generated key. [...]
groupSelectE<TKey, TResult>(Selector<T, TKey> keySelector, GroupSelector<TKey, Iterable<T>, TResult> resultSelector, { EqualityComparer<TKey> keyComparer }) Enumerable<TResult>
Groups the elements in the enumerable by a key and maps the groups to a new element. [...]
groupSelectValueE<TKey, TValue, TResult>(Selector<T, TKey> keySelector, Selector<T, TValue> valueSelector, GroupSelector<TKey, Iterable<TValue>, TResult> resultSelector, { EqualityComparer<TKey> keyComparer }) Enumerable<TResult>
Groups the elements in the enumerable by a key, maps the elements to a new value, and maps the groups to a new element. [...]
intersectE(Iterable<T> other, { EqualityComparer<T> comparer }) Enumerable<T>
Returns the set intersection between the enumerable and the given collection. [...]
joinE<TSecond, TKey, TResult>(Iterable<TSecond> other, Selector<T, TKey> outerKeySelector, Selector<TSecond, TKey> innerKeySelector, ZipSelector<T, TSecond, TResult> selector) Enumerable<TResult>
Finds keys in this enumerable with matching keys in the other collection and returns a value that is the result of the corresponding elements being merged. [...]
lastE({Condition<T> condition }) → T
Returns the last element in the enumerable, optionally matching a specified condition. [...]
lastOrDefaultE({Condition<T> condition, T defaultValue }) → T
Returns the last element in the enumerable, optionally matching a specified condition. [...]
maxE([EqualityComparer<T> comparer ]) → T
Returns the maximum value in the enumerable. [...]
minE([EqualityComparer<T> comparer ]) → T
Returns the minimum value in the enumerable. [...]
ofTypeE<TResult>() Enumerable<TResult>
Returns all elements in the enumerable that are castable to the specified type. [...]
orderByDescendingE<TKey>(Selector<T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) Enumerable<T>
Sorts the enumeration in descending (greatest-to-least) order. [...]
orderByE<TKey>(Selector<T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) Enumerable<T>
Sorts the enumeration in ascending (least-to-greatest) order. [...]
prependE(T newElement) Enumerable<T>
Inserts an element at the beginning of the enumerable. [...]
reverseE() Enumerable<T>
Reverses the order of the enumerable. [...]
selectE<TResult>(Selector<T, TResult> selector) Enumerable<TResult>
Applies a mapping function to the elements in the enumerable. [...]
selectManyE<TResult>(ManySelector<T, TResult> selector) Enumerable<TResult>
Maps elements in an enumerable to collections and then flattens those collections into a single enumerable. [...]
sequenceEqualE(Iterable<T> other, { EqualityComparer<T> comparer }) → bool
Returns true if this enumerable is equivalent to the given collection. [...]
singleE([Condition<T> condition ]) → T
Returns the single element in the element, optionally matching a condition. [...]
singleOrDefaultE(T defaultValue, [ Condition<T> condition ]) → T
Returns the single element in the element, optionally matching a condition, or a defaultValue if no such element exists. [...]
skipE(int count) Enumerable<T>
Skips the first count elements in the enumerable. [...]
skipWhileE(Condition<T> condition) Enumerable<T>
Skips all the elements at the beginning of the enumerable that match the condition. [...]
sumE<TResult extends num>([Selector<T, TResult> selector ]) → TResult
Calculates the sum of the elements in an enumerable, optionally using selector to obtain the value to be summed. [...]
takeE(int count) Enumerable<T>
Takes the first count elements in the enumerable and discards the rest. [...]
takeWhileE(Condition<T> condition) Enumerable<T>
Takes all the elements at the beginning of the enumerable that match the condition and discards the rest. [...]
thenByDescendingE<TKey>(Selector<T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) Enumerable<T>
Adds a secondary sorting pass to enumeration in descending (greatest-to-least) order. [...]
thenByE<TKey>(Selector<T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) Enumerable<T>
Adds a secondary sorting pass to enumeration in ascending (least-to-greatest) order. [...]
toListE({bool growable: false }) → List<T>
Converts the enumerable to a Dart List. [...]
toMapE<TKey, TValue>(Selector<T, TKey> keySelector, Selector<T, TValue> valueSelector) → Map<TKey, TValue>
Converts the enumerable to a Dart Map. [...]
toSetE() → Set<T>
Converts the enumerable to a Dart Set. [...]
unionE(Iterable<T> other, { EqualityComparer<T> comparer }) Enumerable<T>
Returns the set union between the enumerable and the given collection. [...]
whereE(Condition<T> condition) Enumerable<T>
Filters the enumerable to elements that match the condition. [...]
zipE<TSecond, TResult>(Iterable<TSecond> other, ZipSelector<T, TSecond, TResult> selector) Enumerable<TResult>
Combines the values of the enumerable and another collection into an enumerable of new values. [...]
any(bool test(E element)) → bool
Checks whether any element of this iterable satisfies test. [...]
inherited
cast<R>() → Iterable<R>
Provides a view of this iterable as an iterable of R instances. [...]
inherited
contains(Object element) → bool
Returns true if the collection contains an element equal to element. [...]
inherited
elementAt(int index) → T
Returns the indexth element. [...]
inherited
every(bool test(E element)) → bool
Checks whether every element of this iterable satisfies test. [...]
inherited
expand<T>(Iterable<T> f(E element)) → Iterable<T>
Expands each element of this Iterable into zero or more elements. [...]
inherited
firstWhere(bool test(E element), { T orElse() }) → T
Returns the first element that satisfies the given predicate test. [...]
inherited
fold<T>(T initialValue, T combine(T previousValue, E element)) → T
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value [...]
inherited
followedBy(Iterable<T> other) → Iterable<T>
Returns the lazy concatentation of this iterable and other. [...]
inherited
forEach(void f(E element)) → void
Applies the function f to each element of this collection in iteration order.
inherited
join([String separator = "" ]) → String
Converts each element to a String and concatenates the strings. [...]
inherited
lastWhere(bool test(E element), { T orElse() }) → T
Returns the last element that satisfies the given predicate test. [...]
inherited
map<T>(T f(E e)) → Iterable<T>
Returns a new lazy Iterable with elements that are created by calling f on each element of this Iterable in iteration order. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
reduce(T combine(E value, E element)) → T
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function. [...]
inherited
singleWhere(bool test(E element), { T orElse() }) → T
Returns the single element that satisfies test. [...]
inherited
skip(int count) → Iterable<T>
Returns an Iterable that provides all but the first count elements. [...]
inherited
skipWhile(bool test(E value)) → Iterable<T>
Returns an Iterable that skips leading elements while test is satisfied. [...]
inherited
take(int count) → Iterable<T>
Returns a lazy iterable of the count first elements of this iterable. [...]
inherited
takeWhile(bool test(E value)) → Iterable<T>
Returns a lazy iterable of the leading elements satisfying test. [...]
inherited
toList({bool growable: true }) → List<T>
Creates a List containing the elements of this Iterable. [...]
inherited
toSet() → Set<T>
Creates a Set containing the same elements as this iterable. [...]
inherited
toString() → String
Returns a string representation of (some of) the elements of this. [...]
inherited
where(bool test(E element)) → Iterable<T>
Returns a new lazy Iterable with all elements that satisfy the predicate test. [...]
inherited
whereType<T>() → Iterable<T>
Returns a new lazy Iterable with all elements that have type T. [...]
inherited

Operators

operator ==(dynamic other) → bool
The equality operator. [...]
inherited

Static Methods

fromString(String s) Enumerable<String>
Creates an enumerable from a String. [...]
range(int start, int count, { int increment: 1 }) Enumerable<int>
Creates an enumerable containing values composed of the specified range. [...]