sets<K> method

  1. @useResult
Map<K, Set<E>> sets<K>({
  1. required Select<E, K> by,
})

Groups the iterable's elements in sets by keys returned by by.

Example

final list = [('a', 1), ('a', 2), ('b', 4)];
final aggregate = list.group.sets(by: (e) => e.$1);

print(aggregate); // {'a': {('a', 1), ('a', 2)}, 'b': {('b', 4)}}

Implementation details

Computing K is assumed to be cheap. Hence, Ks are recomputed each time rather than cached.

Implementation

@useResult Map<K, Set<E>> sets<K>({required Select<E, K> by}) {
  final results = <K, Set<E>>{};
  for (final element in _iterable) {
    (results[by(element)] ??= {}).add(element);
  }

  return results;
}