mapGrouped<K, G> method

Iterable<G> mapGrouped<K, G>({
  1. required K? groupBy(
    1. T item
    ),
  2. required G groupBuilder(
    1. K? key,
    2. List<T> items
    ),
})

Group elements with the groupBy Function and map those groups with the groupBuilder.

Implementation

Iterable<G> mapGrouped<K, G>({
  required K? Function(T item) groupBy,
  required G Function(K? key, List<T> items) groupBuilder,
}) {
  final grouped = <K?, List<T>>{};
  forEach((item) {
    final key = groupBy(item);
    grouped.putIfAbsent(key, () => []);
    grouped[key]!.add(item);
  });
  return grouped.entries.map((entry) => groupBuilder(entry.key, entry.value));
}