Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KtList.takeLastWhile(predicate) #76

Merged
merged 1 commit into from
Feb 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")));
});
});
}