mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-03-22 15:09:22 +01:00
Iterate with index
This commit is contained in:
parent
60353bc489
commit
01e014b72c
2 changed files with 14 additions and 0 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
extension IterableExtension<T> on Iterable<T> {
|
extension IterableExtension<T> on Iterable<T> {
|
||||||
/// Return a new sorted list
|
/// Return a new sorted list
|
||||||
List<T> sorted([int compare(T a, T b)]) => this.toList()..sort(compare);
|
List<T> sorted([int compare(T a, T b)]) => this.toList()..sort(compare);
|
||||||
|
@ -13,6 +15,8 @@ extension IterableExtension<T> on Iterable<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Iterable<Tuple2<int, T>> withIndex() => mapWithIndex((i, e) => Tuple2(i, e));
|
||||||
|
|
||||||
/// Whether the collection contains an element equal to [element] using the
|
/// Whether the collection contains an element equal to [element] using the
|
||||||
/// equality function [equalFn]
|
/// equality function [equalFn]
|
||||||
bool containsIf(T element, bool Function(T a, T b) equalFn) =>
|
bool containsIf(T element, bool Function(T a, T b) equalFn) =>
|
||||||
|
|
|
@ -20,6 +20,16 @@ void main() {
|
||||||
expect(result[4], Tuple2(4, 3));
|
expect(result[4], Tuple2(4, 3));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("withIndex", () {
|
||||||
|
final src = [1, 4, 5, 2, 3];
|
||||||
|
final result = src.withIndex().toList();
|
||||||
|
expect(result[0], Tuple2(0, 1));
|
||||||
|
expect(result[1], Tuple2(1, 4));
|
||||||
|
expect(result[2], Tuple2(2, 5));
|
||||||
|
expect(result[3], Tuple2(3, 2));
|
||||||
|
expect(result[4], Tuple2(4, 3));
|
||||||
|
});
|
||||||
|
|
||||||
test("containsIf", () {
|
test("containsIf", () {
|
||||||
final src = [
|
final src = [
|
||||||
_ContainsIfTest(1),
|
_ContainsIfTest(1),
|
||||||
|
|
Loading…
Reference in a new issue