groupFoldBy<K, G> method
Groups elements by keyOf
then folds the elements in each group.
A key is found for each element using keyOf
.
Then the elements with the same key are all folded using combine
.
The first call to combine
for a particular key receives null
as
the previous value, the remaining ones receive the result of the previous
call.
Can be used to group elements into arbitrary collections. For example groupSetsBy could be written as:
iterable.groupFoldBy(keyOf,
(Set<T>? previous, T element) => (previous ?? <T>{})..add(element));
Implementation
Map<K, G> groupFoldBy<K, G>(
K Function(T element) keyOf, G Function(G? previous, T element) combine) {
var result = <K, G>{};
for (var element in this) {
var key = keyOf(element);
result[key] = combine(result[key], element);
}
return result;
}