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

Make .flatten() type-safe #56

Merged
merged 3 commits into from
Mar 3, 2020
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
28 changes: 15 additions & 13 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -769,19 +769,6 @@ extension IterableX<E> on Iterable<E> {
}
}

/// Returns a new lazy [Iterable] of all elements from all collections in this
/// collection.
///
/// ```dart
/// var nestedList = List([[1, 2, 3], [4, 5, 6]]);
/// var flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]
/// ```
Iterable<dynamic> flatten() sync* {
for (var current in this) {
yield* (current as Iterable);
}
}

/// Returns a new lazy [Iterable] which iterates over this collection [n]
/// times.
///
Expand Down Expand Up @@ -1005,3 +992,18 @@ extension IterableX<E> on Iterable<E> {
return [t, f];
}
}

extension IterableIterableX<E> on Iterable<Iterable<E>> {
/// Returns a new lazy [Iterable] of all elements from all collections in this
/// collection.
///
/// ```dart
/// var nestedList = List([[1, 2, 3], [4, 5, 6]]);
/// var flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]
/// ```
Iterable<E> flatten() sync* {
for (var current in this) {
yield* current;
}
}
}
20 changes: 20 additions & 0 deletions lib/src/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,23 @@ extension ListX<E> on List<E> {
_mergeSort(this, start: start, end: end, compare: comparator);
}
}

extension ListListX<E> on List<List<E>> {
/// Returns a new [List] of all elements from all lists in this
/// [List].
///
/// ```dart
/// var nestedList = [[1, 2, 3], [4, 5, 6]];
/// var flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]
/// ```
///
///
/// This is a specialization of [IterableIterableX].flatten() which allows
/// accessing elements by index afterwards
///
/// ```dart
/// var flat = [['a', 'b'], ['c', 'd']].flatten();
/// print(flat[2]); // prints "c"
/// ```
List<E> flatten() => [for (var list in this) ...list];
}
36 changes: 27 additions & 9 deletions test/iterable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,33 @@ void main() {
expect([100, 99, 98, 95].asStream(), emitsInOrder([100, 99, 98, 95]));
});

test('.flatten()', () {
expect([].flatten(), []);

var list = [
[0, 0, 0],
[1, 1, 1],
[2, 2, 2]
];
expect(list.flatten(), [0, 0, 0, 1, 1, 1, 2, 2, 2]);
group('.flatten()', () {
test('for iterables of the same type', () {
// ignore: omit_local_variable_types
Iterable<Iterable<int>> iterableOfIterables =
Iterable.generate(3, (index) sync* {
yield index;
yield index + 1;
yield index + 2;
});
// ignore: omit_local_variable_types
Iterable<int> iterable = iterableOfIterables.flatten();
expect(iterable, [0, 1, 2, 1, 2, 3, 2, 3, 4]);
expect(iterable, isA<Iterable<int>>());
expect(iterable.toList(), isA<List<int>>());
});

test('dynamic type', () {
// ignore: omit_local_variable_types
Iterable<Iterable<dynamic>> iterableOfIterables = () sync* {
yield [1, 2, 3];
yield ['a', 'b'];
}();
// ignore: omit_local_variable_types
Iterable<dynamic> iterable = iterableOfIterables.flatten();
expect(iterable, [1, 2, 3, 'a', 'b']);
expect(iterable, isA<Iterable<dynamic>>());
});
});

group('.cycle()', () {
Expand Down
13 changes: 13 additions & 0 deletions test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,18 @@ void main() {
expect(list.dropLastWhile((it) => false), list);
expect(list.dropLastWhile((it) => it > 3), [1, 2, 3]);
});

test('.flatten()', () {
// ignore: omit_local_variable_types
List<List<int>> nestedList = [
[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
];

// ignore: omit_local_variable_types
List<int> flatten = nestedList.flatten();
expect(flatten, [0, 0, 0, 1, 1, 1, 2, 2, 2]);
});
});
}