Skip to content

Commit

Permalink
Merge pull request #76 from passsy/feature/take_last_while
Browse files Browse the repository at this point in the history
KtList.takeLastWhile(predicate)
  • Loading branch information
passsy authored Feb 16, 2019
2 parents 5a051db + 2667e92 commit 06da362
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/src/collection/extension/list_extension_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,27 @@ abstract class KtListExtensionsMixin<T>
}
return list;
}

@override
KtList<T> takeLastWhile(bool Function(T) predicate) {
assert(() {
if (predicate == null) throw ArgumentError("predicate can't be null");
return true;
}());
if (isEmpty()) return emptyList();
final iterator = listIterator(size);
while (iterator.hasPrevious()) {
if (!predicate(iterator.previous())) {
iterator.next();
final expectedSize = size - iterator.nextIndex();
if (expectedSize == 0) return emptyList();
final list = mutableListOf<T>();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
}
return toList();
}
}
5 changes: 5 additions & 0 deletions lib/src/collection/kt_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,9 @@ abstract class KtListExtension<T> {
* Returns a list containing last [n] elements.
*/
KtList<T> takeLast(int n);

/**
* Returns a list containing last elements satisfying the given [predicate].
*/
KtList<T> takeLastWhile(bool Function(T) predicate);
}
2 changes: 1 addition & 1 deletion test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
test("predicate can't be null", () {
final iterable = iterableOf(["abc", "bcd", "cde"]);
final e = catchException<ArgumentError>(() => iterable.all(null));
expect(e.message, allOf(contains("null")));
expect(e.message, allOf(contains("predicate"), contains("null")));
});
});

Expand Down
39 changes: 39 additions & 0 deletions test/collection/list_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,43 @@ void testList(
expect(list.takeLast(2).toList(), listFrom([1, null]));
});
});

group("takeLastWhile", () {
test("take no elements returns empty", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLastWhile((it) => false), emptyList());
});

test("take all elements returns original list", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLastWhile((it) => true), list.toList());
});

if (ordered) {
test("takeLastWhile larger 2", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLastWhile((it) => it > 2), listOf(3, 4));
});
}

if (ordered) {
test("takeLastWhile larger 1", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLastWhile((it) => it > 1), listOf(2, 3, 4));
});
}

if (ordered) {
test("takeLastWhile larger 3", () {
final list = listOf(1, 2, 3, 4);
expect(list.takeLastWhile((it) => it > 3), listOf(4));
});
}

test("predicate can't be null", () {
final list = listOf("a", "b", "c");
final e = catchException<ArgumentError>(() => list.takeLastWhile(null));
expect(e.message, allOf(contains("null"), contains("predicate")));
});
});
}

0 comments on commit 06da362

Please sign in to comment.