IterableUtils<T> extension

Extensions on Iterable for real-world utility operations.

These work on any IterableList, Set, generator sequences, etc.

on

Properties

firstOrNull → T?

Available on Iterable<T>, provided by the IterableUtils extension

Returns the first element, or null if the iterable is empty.
no setter
lastOrNull → T?

Available on Iterable<T>, provided by the IterableUtils extension

Returns the last element, or null if the iterable is empty.
no setter
singleOrNull → T?

Available on Iterable<T>, provided by the IterableUtils extension

Returns the single element, or null if empty or has more than one.
no setter

Methods

associateBy<K>(K key(T)) Map<K, T>

Available on Iterable<T>, provided by the IterableUtils extension

Indexes elements by key, returning a Map<K, T>. If multiple elements share a key, the last one wins.
associateWith<V>(V value(T)) Map<T, V>

Available on Iterable<T>, provided by the IterableUtils extension

Returns a map of each element to the result of value.
averageBy(num f(T)) double

Available on Iterable<T>, provided by the IterableUtils extension

Returns the average of f applied to each element. Returns 0 if empty.
chunked(int size) Iterable<List<T>>

Available on Iterable<T>, provided by the IterableUtils extension

Splits the iterable into chunks of size.
count(bool predicate(T)) int

Available on Iterable<T>, provided by the IterableUtils extension

Returns the number of elements satisfying predicate.
distinctBy<K>(K key(T)) Iterable<T>

Available on Iterable<T>, provided by the IterableUtils extension

Returns distinct elements by key.
flatMap<R>(Iterable<R> f(T)) Iterable<R>

Available on Iterable<T>, provided by the IterableUtils extension

Maps each element to an Iterable and flattens the result.
forEachIndexed(void f(int index, T element)) → void

Available on Iterable<T>, provided by the IterableUtils extension

Calls f for each element with its index.
groupBy<K>(K key(T)) Map<K, List<T>>

Available on Iterable<T>, provided by the IterableUtils extension

Groups elements by key, returning a Map<K, List<T>>.
mapIndexed<R>(R f(int index, T element)) Iterable<R>

Available on Iterable<T>, provided by the IterableUtils extension

Maps each element with its index.
maxBy<R extends Comparable>(R f(T)) → T?

Available on Iterable<T>, provided by the IterableUtils extension

Returns the element with the maximum value of f, or null if empty.
minBy<R extends Comparable>(R f(T)) → T?

Available on Iterable<T>, provided by the IterableUtils extension

Returns the element with the minimum value of f, or null if empty.
none(bool predicate(T)) bool

Available on Iterable<T>, provided by the IterableUtils extension

Returns true if no elements satisfy predicate.
sumBy(num f(T)) num

Available on Iterable<T>, provided by the IterableUtils extension

Returns the sum of f applied to each element.
whereIndexed(bool f(int index, T element)) Iterable<T>

Available on Iterable<T>, provided by the IterableUtils extension

Filters elements with their index.