You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
942 B
47 lines
942 B
4 months ago
|
|
||
|
|
||
|
|
||
|
Map<S, List<T>> groupBy<S, T>(Iterable<T> values, S Function(T) key) {
|
||
|
var map = <S, List<T>>{};
|
||
|
for (var element in values) {
|
||
|
(map[key(element)] ??= []).add(element);
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
Map<String, int> groupCount<S, T>(Map<S, List<T>> values) {
|
||
|
var map = <String, int>{};
|
||
|
for (var element in values.keys) {
|
||
|
map["$element"] = values[element]?.length ?? 0;
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
Map<String, T> groupItem<S, T>(Map<S, List<T>> values) {
|
||
|
var map = <String, T>{};
|
||
|
for (var element in values.keys) {
|
||
|
if (values[element] == null) {
|
||
|
continue;
|
||
|
}
|
||
|
map["$element"] = values[element].first;
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
|
||
|
extension ListExtension<S, T> on Iterable<T> {
|
||
|
|
||
|
Map<S, List<T>> lGroupBy(S Function(T) key) {
|
||
|
return groupBy(this, key);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
extension MapExtension<S, T> on Map<S, List<T>> {
|
||
|
|
||
|
Map<String, int> get mGroupCount => groupCount(this);
|
||
|
|
||
|
Map<String, T> get mGroupItem => groupItem(this);
|
||
|
|
||
|
}
|