Skip to content

Commit

Permalink
Add flattenedToList and flattenedToSet (#328)
Browse files Browse the repository at this point in the history
For iterables which are known to be exhausted after flattening
performance is better than the `sync*` implementation using a collection
literal.

Add `flattenedToList` as a performance improvement over `flattened.`
Add `flattenedToSet` as new behavior for flattening to unique elements.

Originally implemented in
dart-lang/sdk@8d3b6ce
  • Loading branch information
natebosch committed Jun 10, 2024
1 parent e9219c7 commit a1d2507
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
generic.
- `CanonicalizedMap`: added constructor `fromEntries`.
- Mark "mixin" classes as `mixin`.
- `extension IterableIterableExtension<T> on Iterable<Iterable<T>>`
- Add `flattenedToList` as a performance improvement over `flattened.`
- Add `flattenedToSet` as new behavior for flattening to unique elements.
- Deprecate `transitiveClosure`. Consider using `package:graphs`.
- Deprecate `whereNotNull()` from `IterableNullableExtension`. Use `nonNulls`
instead - this is an equivalent extension available in Dart core since
Expand Down
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
57 changes: 57 additions & 0 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,63 @@ 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});
expect(
iterable(<Iterable>[
iterable([1, 2, 3]),
empty,
iterable([2, 3, 4])
]).flattenedToSet,
{1, 2, 3, 4});
});
});
});
group('of comparable', () {
group('.min', () {
Expand Down

0 comments on commit a1d2507

Please sign in to comment.