-
Notifications
You must be signed in to change notification settings - Fork 34
Add separated, separatedList and separate to iterables and lists.
#919
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
Changes from 1 commit
57adf31
fe35c56
c82e508
4898814
640a530
9fb1d44
f2bcde8
9db5e50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,102 @@ extension IterableExtension<T> on Iterable<T> { | |
| return chosen; | ||
| } | ||
|
|
||
| /// The elements of this iterable separated by [separator]. | ||
| /// | ||
| /// Emits the same elements as this iterable, and also emits | ||
| /// a [separator] between any two of those elements. | ||
| /// | ||
| /// If [before] is set to `true`, a [separator] is also | ||
| /// emitted before the first element. | ||
| /// If [after] is set to `true`, a [separator] is also | ||
| /// emitted after the last element. | ||
| /// | ||
| /// If this iterable is empty, [before] and [after] have no effect. | ||
| /// | ||
| /// Example: | ||
| /// ```dart | ||
| /// print([1, 2, 3].separated(-1)); // (1, -1, 2, -1, 3) | ||
| /// print([1].separated(-1)); // (1) | ||
| /// print([].separated(-1)); // () | ||
| /// | ||
| /// print([1, 2, 3].separated( | ||
| /// -1 | ||
| /// before: true, | ||
| /// )); // (-1, 1, -1, 2, -1, 3) | ||
| /// | ||
| /// print([1].separated( | ||
| /// -1 | ||
| /// before: true, | ||
| /// after: true, | ||
| /// )); // (-1, 1, -1) | ||
| /// | ||
| /// print([].separated( | ||
| /// -1 | ||
| /// before: true, | ||
| /// after: true, | ||
| /// )); // () | ||
| /// ``` | ||
| Iterable<T> separated(T separator, | ||
lrhn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {bool before = false, bool after = false}) sync* { | ||
|
||
| const emitBefore = 1; | ||
| const emitAfter = 2; | ||
| var state = before ? emitBefore : 0; | ||
| final afterState = emitBefore | (after ? emitAfter : 0); | ||
| for (var element in this) { | ||
| if (state & emitBefore != 0) yield separator; | ||
| state = afterState; | ||
| yield element; | ||
| } | ||
| if (state & emitAfter != 0) yield separator; | ||
| } | ||
|
|
||
| /// Creates new list with the elements of this list separated by [separator]. | ||
| /// | ||
| /// Returns a new list which contains the same elements as this list, | ||
| /// with a [separator] between any two of those elements. | ||
| /// | ||
| /// If [before] is set to `true`, a [separator] is also | ||
| /// added before the first element. | ||
| /// If [after] is set to `true`, a [separator] is also | ||
| /// added after the last element. | ||
| /// | ||
| /// If this list is empty, [before] and [after] have no effect. | ||
lrhn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// Example: | ||
| /// ```dart | ||
| /// print([1, 2, 3].separatedList(-1)); // [1, -1, 2, -1, 3] | ||
| /// print([1].separatedList(-1)); // [1] | ||
| /// print([].separatedList(-1)); // [] | ||
| /// | ||
| /// print([1, 2, 3].separatedList( | ||
| /// -1 | ||
| /// before: true, | ||
| /// )); // [-1, 1, -1, 2, -1, 3] | ||
| /// | ||
| /// print([1].separatedList( | ||
| /// -1 | ||
| /// before: true, | ||
| /// after: true, | ||
| /// )); // [-1, 1, -1] | ||
| /// | ||
| /// print([].separatedList( | ||
| /// -1 | ||
| /// before: true, | ||
| /// after: true, | ||
| /// )); // [] | ||
| /// ``` | ||
| List<T> separatedList(T separator, | ||
| {bool before = false, bool after = false}) { | ||
| var hasElement = false; | ||
| return [ | ||
| for (var element in this) ...[ | ||
| if (hasElement || (hasElement = true) && before) separator, | ||
| element, | ||
| ], | ||
| if (hasElement && after) separator | ||
| ]; | ||
| } | ||
|
|
||
| /// The elements that do not satisfy [test]. | ||
| Iterable<T> whereNot(bool Function(T element) test) => | ||
| where((element) => !test(element)); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.