Skip to content

Commit

Permalink
Return a non-nullable type for *NotNull methods
Browse files Browse the repository at this point in the history
filterNotNull, filterNotNullTo, mapIndexedNotNull, mapIndexedNotNullTo, mapNotNull, mapNotNullTo

Fixes #144
  • Loading branch information
passsy committed Aug 15, 2021
1 parent 9284057 commit f43cbc5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 96 deletions.
166 changes: 83 additions & 83 deletions lib/src/collection/kt_iterable.dart
Expand Up @@ -498,40 +498,6 @@ extension KtIterableExtensions<T> on KtIterable<T> {
return list;
}

/// Returns a list containing all elements that are not `null`.
KtList<T> filterNotNull() {
final list = filterNotNullTo(mutableListOf<T>());
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"
return list;
}

/// Appends all elements that are not `null` to the given [destination].
///
/// [destination] is not type checked by the compiler due to https://github.com/dart-lang/sdk/issues/35518,
/// but will be checked at runtime.
/// [C] actually is expected to be `C extends KtMutableCollection<T>`
// TODO Change to `C extends KtMutableCollection<T>` once https://github.com/dart-lang/sdk/issues/35518 has been fixed
C filterNotNullTo<C extends KtMutableCollection<dynamic>>(C destination) {
assert(() {
if (destination is! KtMutableCollection<T> && mutableListOf<T>() is! C) {
throw ArgumentError(
"filterNotNullTo destination has wrong type parameters."
"\nExpected: KtMutableCollection<$T>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
}
return true;
}());
for (final element in iter) {
if (element != null) {
destination.add(element);
}
}
return destination;
}

/// Appends all elements not matching the given [predicate] to the given [destination].
///
/// [destination] is not type checked by the compiler due to https://github.com/dart-lang/sdk/issues/35518,
Expand Down Expand Up @@ -946,33 +912,6 @@ extension KtIterableExtensions<T> on KtIterable<T> {
return mapped;
}

/// Returns a list containing only the non-null results of applying the given [transform] function
/// to each element and its index in the original collection.
/// @param [transform] function that takes the index of an element and the element itself
/// and returns the result of the transform applied to the element.
KtList<R> mapIndexedNotNull<R>(R Function(int index, T) transform) {
final mapped = mapIndexedNotNullTo(mutableListOf<R>(), transform);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"
return mapped;
}

/// Applies the given [transform] function to each element and its index in the original collection
/// and appends only the non-null results to the given [destination].
/// @param [transform] function that takes the index of an element and the element itself
/// and returns the result of the transform applied to the element.
C mapIndexedNotNullTo<R, C extends KtMutableCollection<R>>(
C destination, R Function(int index, T) transform) {
var index = 0;
for (final item in iter) {
final element = transform(index++, item);
if (element != null) {
destination.add(element);
}
}
return destination;
}

/// Applies the given [transform] function to each element and its index in the original collection
/// and appends the results to the given [destination].
/// @param [transform] function that takes the index of an element and the element itself
Expand All @@ -986,28 +925,6 @@ extension KtIterableExtensions<T> on KtIterable<T> {
return destination;
}

/// Returns a list containing the results of applying the given [transform] function
/// to each element in the original collection.
KtList<R> mapNotNull<R>(R Function(T) transform) {
final mapped = mapNotNullTo(mutableListOf<R>(), transform);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"
return mapped;
}

/// Applies the given [transform] function to each element in the original collection
/// and appends only the non-null results to the given [destination].
C mapNotNullTo<R, C extends KtMutableCollection<R>>(
C destination, R Function(T) transform) {
for (final item in iter) {
final result = transform(item);
if (result != null) {
destination.add(result);
}
}
return destination;
}

/// Applies the given [transform] function to each element of the original collection
/// and appends the results to the given [destination].
C mapTo<R, C extends KtMutableCollection<R>>(
Expand Down Expand Up @@ -1498,6 +1415,89 @@ extension RequireNoNullsKtIterableExtension<T> on KtIterable<T?> {
}
return cast<T>();
}

/// Returns a list containing all elements that are not `null`.
KtList<T> filterNotNull() {
final list = filterNotNullTo(mutableListOf<T>());
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"
return list;
}

/// Appends all elements that are not `null` to the given [destination].
///
/// [destination] is not type checked by the compiler due to https://github.com/dart-lang/sdk/issues/35518,
/// but will be checked at runtime.
/// [C] actually is expected to be `C extends KtMutableCollection<T>`
// TODO Change to `C extends KtMutableCollection<T>` once https://github.com/dart-lang/sdk/issues/35518 has been fixed
C filterNotNullTo<C extends KtMutableCollection<dynamic>>(C destination) {
assert(() {
if (destination is! KtMutableCollection<T> && mutableListOf<T>() is! C) {
throw ArgumentError(
"filterNotNullTo destination has wrong type parameters."
"\nExpected: KtMutableCollection<$T>, Actual: ${destination.runtimeType}"
"\ndestination (${destination.runtimeType}) entries aren't subtype of "
"map ($runtimeType) entries. Entries can't be copied to destination."
"\n\n$kBug35518GenericTypeError");
}
return true;
}());
for (final element in iter) {
if (element != null) {
destination.add(element);
}
}
return destination;
}

/// Returns a list containing only the non-null results of applying the given [transform] function
/// to each element and its index in the original collection.
/// @param [transform] function that takes the index of an element and the element itself
/// and returns the result of the transform applied to the element.
KtList<R> mapIndexedNotNull<R>(R? Function(int index, T?) transform) {
final mapped = mapIndexedNotNullTo(mutableListOf<R>(), transform);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"
return mapped;
}

/// Applies the given [transform] function to each element and its index in the original collection
/// and appends only the non-null results to the given [destination].
/// @param [transform] function that takes the index of an element and the element itself
/// and returns the result of the transform applied to the element.
C mapIndexedNotNullTo<R, C extends KtMutableCollection<R>>(
C destination, R? Function(int index, T?) transform) {
var index = 0;
for (final item in iter) {
final element = transform(index++, item);
if (element != null) {
destination.add(element);
}
}
return destination;
}

/// Returns a list containing the results of applying the given [transform] function
/// to each element in the original collection.
KtList<R> mapNotNull<R>(R? Function(T?) transform) {
final mapped = mapNotNullTo(mutableListOf<R>(), transform);
// TODO ping dort-lang/sdk team to check type bug
// When in single line: type "DartMutableList<String>' is not a subtype of type 'Null"
return mapped;
}

/// Applies the given [transform] function to each element in the original collection
/// and appends only the non-null results to the given [destination].
C mapNotNullTo<R, C extends KtMutableCollection<R>>(
C destination, R? Function(T?) transform) {
for (final item in iter) {
final result = transform(item);
if (result != null) {
destination.add(result);
}
}
return destination;
}
}

// TODO: Should be <T, C extends KtIterable<T>> on C but then T resolves to dynamic
Expand Down
26 changes: 13 additions & 13 deletions test/collection/iterable_extensions_test.dart
Expand Up @@ -700,17 +700,18 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,

group("filterNotNull", () {
test("filterNotNull", () {
final iterable = iterableOf(["paul", null, "john", "lisa"]);
expect(iterable.filterNotNull().toSet(),
equals(setOf("paul", "john", "lisa")));
final KtIterable<String?> iterable =
iterableOf(["paul", null, "john", "lisa"]);
final KtSet<String> set = iterable.filterNotNull().toSet();
expect(set, equals(setOf("paul", "john", "lisa")));
});
});

group("filterNotNullTo", () {
test("filterNotNullTo same type", () {
final iterable = iterableOf([4, 25, null, 10]);
final result = mutableListOf<int?>();
final filtered = iterable.filterNotNullTo(result);
final KtIterable<int?> iterable = iterableOf([4, 25, null, 10]);
final result = mutableListOf<int>();
final KtMutableList<int> filtered = iterable.filterNotNullTo(result);
expect(identical(result, filtered), isTrue);
if (ordered) {
expect(result, listOf(4, 25, 10));
Expand Down Expand Up @@ -1301,13 +1302,12 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
if (ordered) {
group("mapIndexedNotNull", () {
test("mapIndexedNotNull int to string", () {
final iterable = iterableOf(["a", null, "b", "c"]);
expect(
iterable.mapIndexedNotNull((index, it) {
if (it == null) return null;
return "$index$it";
}).toList(),
listOf("0a", "2b", "3c"));
final KtIterable<String?> iterable = iterableOf(["a", null, "b", "c"]);
final KtList<String> result = iterable.mapIndexedNotNull((index, it) {
if (it == null) return null;
return "$index$it";
}).toList();
expect(result, listOf("0a", "2b", "3c"));
});
});
}
Expand Down

0 comments on commit f43cbc5

Please sign in to comment.