groupBy<A, K> function
- K keyFunc(
- A
Groups items in an ImmutableList by a key produced by the keyFunc
function.
Example:
final list = ImmutableList<String>(["apple", "banana", "avocado"]);
final grouped = groupBy<String, int>((fruit) => fruit.length)(list);
print(grouped); // Outputs: {5: [apple], 6: [banana, avocado]}
Implementation
Map<K, ImmutableList<A>> Function(ImmutableList<A>) groupBy<A, K>(
K Function(A) keyFunc) =>
(ImmutableList<A> list) {
Map<K, List<A>> tempMap = SplayTreeMap();
for (var item in list._items) {
final key = keyFunc(item);
tempMap[key] ??= [];
tempMap[key]!.add(item);
}
return tempMap.map((key, items) => MapEntry(key, ImmutableList(items)));
};