pub package

A port of .NET's LINQ IEnumerable functions to Dart. This library adds a powerful Enumerable collection type to Dart that greatly increases convenience when modifying a collection as well as performance when dealing with large collections and complex modifications.

API Reference

Lazy Execution

The power of Enumerable is that it supports modification to an underlying collection using lazy execution. This means that the enumerable only stores the data necessary to perform the modification and does not actually perform the modification until the enumerable is iterated over. No data is cached either, so unless you actually iterate over the enumerable, creating one is virtually free from both a memory and performance standpoint.

What's more, when multiple enumerable modifications are chained together, the iteration is done through them all simultaneously. This greatly eases the overhead of performing complex modifications on large collection sets.

Usage

An Enumerable can be created out of any Dart collection type that extends Iterable. There are two ways to do so:

var sourceList = [0, 1, 2, 3, 4];

// Factory method
var factoryEnum = Enumerable.from(sourceList);

// Convenience method
var convenientEnum = E(sourceList);

You can also generate an Enumerable without needing a pre-existing collection using one of several factory methods:

// Creates an Enumerable with no values
var emptyEnum = Enumerable.empty();

// Creates an Enumerable containing 5 integers starting with the number 2
var rangeEnum = Enumerable.range(2, 5);

// Creates an Enumerable that contains 6 copies of the value 'a string'
var repeatEnum = Enumerable.repeat('a string', 6);

// Creates an Enumerable from a string, iterating over its characters
var stringEnum = Enumerable.fromString('abcdef');

// Creates an Enumerable using a generator function
var generatedEnum = Enumerable.generate(5, (index) => (index * 2).toString());

Once you have an Enumerable, you can call any of 50 different methods on it to modify or analyze it. For example, you can map to a new value with selectE:

var myEnum = E([1, 2, 3]);
var mappedEnum = myEnum.selectE((i) => i * 2);
// Values: [2, 4, 6]

...filter the elements with whereE:

var myEnum = E([1, 2, 3]);
var filteredEnum = myEnum.whereE((i) => i.isOdd);
// Values: [1, 3]

...get only unique values with distinctE:

var myEnum = E([1, 1, 1, 2, 2, 3, 4, 5, 5, 5, 5, 5]);
var uniqueEnum = myEnum.distinctE();
// Values: [1, 2, 3, 4, 5]

...or even group elements together using groupByE:

var myEnum = E([1, 2, 3, 4, 5, 6]);
var groupedEnum = myEnum.groupByE((i) => i % 2);
// Values: [[1, 3, 5], [2, 4, 6]]

What's more, you can chain methods together, enabling virtually endless possibilities in a concise chain of method calls:

var myEnum = E([1, 2, 3, 4, 5, 6]);
var resultEnum = myEnum.selectE((i) => i * 2)
                       .whereE((i) => i > 4)
                       .selectE((i) => i.toRadixString(16));
/// Values: ['6', '8', 'A', 'C']

To use the values, you can iterate over the Enumerable just like you would any other Iterable collection:

var myEnum = E([1, 2, 3]);
for (var value in myEnum) {
    print(value);
}

// Output:
// 1
// 2
// 3

You can also easily convert the Enumerable back into a Dart collection type using toListE, toMapE, or toSetE:

var myEnum = E([1, 2, 3]);
var myList = myEnum.ToList();
// myList is a List<int> with the values of myEnum

Full Function List

Libraries

darq