nc-photos/lib/list_extension.dart

20 lines
651 B
Dart
Raw Normal View History

import 'package:nc_photos/override_comparator.dart';
2021-04-10 06:28:12 +02:00
extension ListExtension<T> on List<T> {
/// Return a new list with only distinct elements
List<T> distinct() {
2021-09-15 08:58:06 +02:00
final s = <T>{};
return where((element) => s.add(element)).toList();
2021-04-10 06:28:12 +02:00
}
/// Return a new list with only distinct elements determined by [equalFn]
List<T> distinctIf(
bool Function(T a, T b) equalFn, int Function(T a) hashCodeFn) {
final s = <OverrideComparator<T>>{};
2021-09-15 08:58:06 +02:00
return where((element) =>
s.add(OverrideComparator<T>(element, equalFn, hashCodeFn))).toList();
}
2021-04-10 06:28:12 +02:00
Iterable<T> takeIndex(List<int> indexes) => indexes.map((e) => this[e]);
}