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

separated method added in Iterable extension #343

Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.19.0-wip

- Adds `shuffled` to `IterableExtension`.
- Adds `separated` to `IterableExtension`.
- Shuffle `IterableExtension.sample` results.
- Fix `mergeSort` when the runtime iterable generic is a subtype of the static
generic.
Expand Down
21 changes: 21 additions & 0 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ extension IterableExtension<T> on Iterable<T> {
return chosen;
}

/// Separates the elements of the iterable with the specified separator.
///
/// The [separated] method takes a separator as input and returns a new
/// iterable where the elements are separated by the specified separator.
///
/// Example:
/// ```dart
/// final list = [1, 2, 3, 4];
/// final separatedList = list.separated(0);
/// print(separatedList); // Output: [1, 0, 2, 0, 3, 0, 4]
/// ```
Iterable<T> separated(T separator) sync* {
final iterator = this.iterator;
if (!iterator.moveNext()) return;
yield iterator.current;
while (iterator.moveNext()) {
yield separator;
yield iterator.current;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this version is flexible enough.
The use-cases I've heard mentioned won't all be satisfies by inserting the same value every time, and the typing might not work out.

I'd at least want a version where the separator is generated each time.
Then it could be generated based on position, or on adjacent values.

Something like these would cover those:

Iterable<T> separatedByValue(T separator) { ... }
Iterable<T> separatedBy(T Function() separator) { ... }

and possibly also:

// Position is index of next element, from 1 to `lenght` - 1.
Iterable<T> separatedByIndex(T Function(int position) separator) { ... }
Iterable<T> separatedByElements(T Function(T before, T after) separator) { ... }

The separatedBy and separatedByIndex might be combined into one, it's easy to ignore an argument, and cheap to increment an integer.

I'd expect the separator to not always have the same type as the elements being separated. (A List<TextWidgets> cannot be separated by SeparatorWidgets, one would have to first copy the text widgets into a List<Widget> and then separate them)

That makes a top-level function more useful (or stastic function or constructor, if we could add such using extensions):

  static Iterable<S> separatedBy<S>(Iterable<S> elements, S Function(int position)  separator) { ... }
  static Iterable<S> separatedByValue<S>(Iterable<S> elements, S  separator) { ... }
  static Iterable<S> separatedByElements<S, T extends S>(Iterable<T> elements, S Function(T before, T after)  separator) { ... }

Maybe we can make the separator optional, like:

  static Iterable<S> separatedOptionalBy<S>(Iterable<S> elements, S? Function(int position)  separator) { ... }
  static Iterable<S> separatedOptionalByElements<S, T extends S>(Iterable<T> elements, S? Function(T before, T after)  separator) { ... }

where returning null means no separator.
(Consider: separatedOptionalByElements(pathSegments, (before, after) => before.endsWith('/') || after.startsWith('/') ? null : '/') where we only insert a separator where it's needed. Maybe this is too specialized, and a join with conditional separator would be more relevant.)


/// The elements that do not satisfy [test].
Iterable<T> whereNot(bool Function(T element) test) =>
where((element) => !test(element));
Expand Down
19 changes: 19 additions & 0 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,25 @@ void main() {
]);
});
});
group('.separated', () {
test('empty', () {
expect(iterable([]).separated(unreachable), isEmpty);
});

test('single', () {
expect(iterable([1]).separated(0), [1]);
});

test('multiple', () {
expect(iterable([1, 2, 3]).separated(0), [1, 0, 2, 0, 3]);
expect(iterable(['a', 'b']).separated('-'), ['a', '-', 'b']);
});

test('dynamic', () {
expect(iterable<Object>([1, '2', 3]).separated(0), [1, 0, '2', 0, 3]);
expect(iterable<Object>(['a', 'b']).separated(0), ['a', 0, 'b']);
});
});
group('none', () {
test('empty', () {
expect(iterable([]).none(unreachable), true);
Expand Down