groupBy<A, K> function
- K keyFunc(
- A
Groups items by a derived key, as determined by the keyFunc
.
var nel = NonEmptyList(["one", "two", "three"]);
var grouped = groupBy<String, int>((s) => s.length)(nel);
print(grouped[3]!.items); // ["one", "two"]
Implementation
Map<K, NonEmptyList<A>> Function(NonEmptyList<A>) groupBy<A, K>(
K Function(A) keyFunc) =>
(NonEmptyList<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, NonEmptyList(items)));
};