-
Notifications
You must be signed in to change notification settings - Fork 7
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
[Proposal] add mapSeparated method on Iterable
#674
Comments
After some discussion on the FlutterDev Discord server, a potential issue with A solution would be to instead add a simpler Sample implementation: extension <T> on Iterable<T> {
Iterable<T> separate(T separator) sync* {
final iterator = this.iterator;
if (!iterator.moveNext()) return;
yield iterator.current;
while (iterator.moveNext()) {
yield separator;
yield iterator.current;
}
}
} |
A problem with this design is that if you have a That's not particularly convenient, a top level function taking both as arguments seems better. Another issue is that you may not want to use the same instance as separator, and may want to create a new instance for each gap. The most general version would probably be: Iterable<E> separated<E>(Iterable<E> elements, Iterable<E> separators) sync* {
var it = elements.iterator;
if (!it.moveNext()) return;
yield it.current;
if (!it.moveNext()) return;
for (var sep in separators) {
yield sep;
yield it.current;
if (!it.moveNext()) return;
}
do {
yield it.current;
} while (it.moveNext());
} which separates |
FWIW I think the idiomatic Flutter solution to that is that you should make the list's type be There can be exceptions to that pattern, but the fact they're uncommon may make it OK if they're slightly clunky to use with this method. |
I'm not sure how often one would have a use case for letting Conversely, if one does have a use case for separating the first (Maybe this is ultimately an argument for having it take a factory function, with the position as argument, instead of an iterable.) |
Current implementation using an extension method
I chose to add this method as an
extension
of theIterable
type so I can use it for bothList
andItrables
. I mostly use this withList
type and because of that I have to use either the spread operator to add output in anotherList
or use it withtoList()
. We should discuss if this should stay as it is or we should add this only forList
.Use case:
Sometimes we want that list out our
List
orIterable
with something in between. For example, we have aList
ofWidget
to use in theColumn
orRow
withSizedBox(height: 10)
but we are not usingListView.separated
for some reason. There this can be useful!Also, it can be useful when we are not sure whether each element of the
Column
orRow
is displayed or not based on some condition and we want to add some space between two children.eg.
Notice that I have used
Iterable<S>
, this can be useful when we have aList
of typeA
and we wantList
of typeB
as output with separators. In the below exampleS
is({String text, Information info})
and We are usingmapSeparated
method to show all clickableTextSpan
inRichText
with,
in between.If anyone has any different views or take on this please share! I was using this with almost all of my projects hence I thought it should be added in here.
The text was updated successfully, but these errors were encountered: