group<T, K> function

Map<K, List<T>> group<T, K>(
  1. Iterable<T> iterable,
  2. K key(
    1. T
    )
)

Groups elements by a key function.

Implementation

Map<K, List<T>> group<T, K>(Iterable<T> iterable, K Function(T) key) {
  final map = <K, List<T>>{};
  for (final item in iterable) {
    final k = key(item);
    (map[k] ??= []).add(item);
  }
  return map;
}