extension ListExtension on List { /// Return a new list with only distinct elements List distinct() { final s = {}; return where((element) => s.add(element)).toList(); } /// Return a new list with only distinct elements determined by [equalFn] List distinctIf( bool Function(T a, T b) equalFn, int Function(T a) hashCodeFn) { final s = <_DistinctComparator>{}; return where((element) => s.add(_DistinctComparator(element, equalFn, hashCodeFn))).toList(); } Iterable takeIndex(List indexes) => indexes.map((e) => this[e]); } class _DistinctComparator { _DistinctComparator(this.obj, this.equalFn, this.hashCodeFn); @override operator ==(Object other) => other is _DistinctComparator && equalFn(obj, other.obj); @override get hashCode => hashCodeFn(obj); final T obj; final bool Function(T, T) equalFn; final int Function(T) hashCodeFn; }