From d641ed1936151b907aa0434ff5a425056eb611e9 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Fri, 12 Jan 2024 04:23:09 +0000 Subject: [PATCH 1/2] Add flattenedToList and flattenedToSet 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 https://github.com/dart-lang/sdk/commit/8d3b6ce54ccb09dd970cd45fc3cded4a35beac31 --- lib/src/iterable_extensions.dart | 20 +++++++++++++ test/extensions_test.dart | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/lib/src/iterable_extensions.dart b/lib/src/iterable_extensions.dart index 1bf4b3e..97df256 100644 --- a/lib/src/iterable_extensions.dart +++ b/lib/src/iterable_extensions.dart @@ -891,6 +891,26 @@ extension IterableIterableExtension on Iterable> { 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 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 get flattenedToSet => { + for (final elements in this) ...elements, + }; } /// Extensions that apply to iterables of [Comparable] elements. diff --git a/test/extensions_test.dart b/test/extensions_test.dart index 3b1401a..5f2d233 100644 --- a/test/extensions_test.dart +++ b/test/extensions_test.dart @@ -1034,6 +1034,56 @@ void main() { [1, 2, 3, 4]); }); }); + group('.flattenedToList', () { + var empty = iterable([]); + test('empty', () { + expect(iterable(>[]).flattenedToList, []); + }); + test('multiple empty', () { + expect(iterable([empty, empty, empty]).flattenedToList, []); + }); + test('single value', () { + expect( + iterable([ + iterable([1]) + ]).flattenedToList, + [1]); + }); + test('multiple', () { + expect( + iterable([ + iterable([1, 2]), + empty, + iterable([3, 4]) + ]).flattenedToList, + [1, 2, 3, 4]); + }); + }); + group('.flattenedToSet', () { + var empty = iterable([]); + test('empty', () { + expect(iterable(>[]).flattenedToSet, {}); + }); + test('multiple empty', () { + expect(iterable([empty, empty, empty]).flattenedToSet, {}); + }); + test('single value', () { + expect( + iterable([ + iterable([1]) + ]).flattenedToSet, + {1}); + }); + test('multiple', () { + expect( + iterable([ + iterable([1, 2]), + empty, + iterable([3, 4]) + ]).flattenedToSet, + {1, 2, 3, 4}); + }); + }); }); group('of comparable', () { group('.min', () { From bd3dec9cfeb8d183a2c27bc47e918cd7d74a2a04 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Mon, 10 Jun 2024 14:10:58 -0700 Subject: [PATCH 2/2] Add changelog and duplicate test --- CHANGELOG.md | 3 +++ test/extensions_test.dart | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f93bdbc..d4b8cf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ generic. - `CanonicalizedMap`: added constructor `fromEntries`. - Mark "mixin" classes as `mixin`. +- `extension IterableIterableExtension on Iterable>` + - 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 diff --git a/test/extensions_test.dart b/test/extensions_test.dart index 8be7238..9940e1d 100644 --- a/test/extensions_test.dart +++ b/test/extensions_test.dart @@ -1112,6 +1112,13 @@ void main() { iterable([3, 4]) ]).flattenedToSet, {1, 2, 3, 4}); + expect( + iterable([ + iterable([1, 2, 3]), + empty, + iterable([2, 3, 4]) + ]).flattenedToSet, + {1, 2, 3, 4}); }); }); });