Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Add flattenedToList and flattenedToSet #328

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,26 @@ extension IterableIterableExtension<T> on Iterable<Iterable<T>> {
yield* elements;
}
}

/// The sequential elements of each iterable in this iterable.
///
/// Iterates the elements of this iterable.
/// For each one, which is itself an iterable,
/// all the elements of that are added
/// to the returned list, before moving on to the next element.
List<T> get flattenedToList => [
for (final elements in this) ...elements,
];

/// The unique sequential elements of each iterable in this iterable.
///
/// Iterates the elements of this iterable.
/// For each one, which is itself an iterable,
/// all the elements of that are added
/// to the returned set, before moving on to the next element.
Set<T> get flattenedToSet => {
for (final elements in this) ...elements,
};
}

/// Extensions that apply to iterables of [Comparable] elements.
Expand Down
50 changes: 50 additions & 0 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,56 @@ void main() {
[1, 2, 3, 4]);
});
});
group('.flattenedToList', () {
var empty = iterable(<int>[]);
test('empty', () {
expect(iterable(<Iterable<int>>[]).flattenedToList, []);
});
test('multiple empty', () {
expect(iterable([empty, empty, empty]).flattenedToList, []);
});
test('single value', () {
expect(
iterable(<Iterable>[
iterable([1])
]).flattenedToList,
[1]);
});
test('multiple', () {
expect(
iterable(<Iterable>[
iterable([1, 2]),
empty,
iterable([3, 4])
]).flattenedToList,
[1, 2, 3, 4]);
});
});
group('.flattenedToSet', () {
var empty = iterable(<int>[]);
test('empty', () {
expect(iterable(<Iterable<int>>[]).flattenedToSet, <int>{});
});
test('multiple empty', () {
expect(iterable([empty, empty, empty]).flattenedToSet, <int>{});
});
test('single value', () {
expect(
iterable(<Iterable>[
iterable([1])
]).flattenedToSet,
{1});
});
test('multiple', () {
expect(
iterable(<Iterable>[
iterable([1, 2]),
empty,
iterable([3, 4])
]).flattenedToSet,
{1, 2, 3, 4});
kevmoo marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
group('of comparable', () {
group('.min', () {
Expand Down