groupAndTransformBy<K, V> method

Map<K, Iterable<V>> groupAndTransformBy<K, V>(
  1. K keySelector(
    1. E element
    ),
  2. V valueTransform(
    1. E element
    )
)

Groups values returned by the valueTransform function applied to each element of the original collection by the key returned by the given keySelector function applied to the element and returns a Map where each group key is associated with a Iterable of corresponding values.

Implementation

Map<K, Iterable<V>> groupAndTransformBy<K, V>(
  K Function(E element) keySelector,
  V Function(E element) valueTransform,
) {
  final groups = <K, Iterable<V>>{};

  for (final element in this) {
    final key = keySelector(element);

    groups.putIfAbsent(
      key,
      () => where((element) => keySelector(element) == key)
          .map((element) => valueTransform(element)),
    );
  }

  return groups;
}