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.

65 lines
1.3 KiB

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;
}
4 months ago
Map<String, T> groupItem<S, T>(Map<S, List<T>> values, {int Function(T) key}) {
4 months ago
var map = <String, T>{};
for (var element in values.keys) {
if (values[element] == null) {
continue;
}
4 months ago
map["$element"] = key == null ? values[element].first : values[element].lMax(key);
4 months ago
}
return map;
}
4 months ago
T max<T>(Iterable<T> list, int Function(T) key) {
T tt;
for (T t in list) {
if (tt == null) {
tt = t;
}
if (key(tt) < key(t)) {
tt = t;
}
}
return tt;
}
4 months ago
extension ListExtension<S, T> on Iterable<T> {
Map<S, List<T>> lGroupBy(S Function(T) key) {
return groupBy(this, key);
}
4 months ago
T lMax(int Function(T) key) {
return max(this, key);
}
4 months ago
}
extension MapExtension<S, T> on Map<S, List<T>> {
Map<String, int> get mGroupCount => groupCount(this);
4 months ago
Map<String, T> mGroupItem({int Function(T) key}) {
return groupItem(this, key: key);
}
4 months ago
}