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

RFC: Add enumerated extension #259

Closed

Conversation

lukepighetti
Copy link

@lukepighetti lukepighetti commented Dec 21, 2022

I have found this extension to be useful, it allows someone to skip the common for loop pattern for accessing elements. It also supersedes the need for specialized methods like mapIndexed, whereIndexed, (I count 14 in this package). BUT the value seems low because technically we have asMap and these specialized methods already

Before

for(var i = 0; i < foo.length; i++){
  print(i);
  print(foo[i]);
}
foo.forEach((value){
  final index = foo.indexOf(value);
  print(index);
  print(value);
}

After

for(final element in foo.enumerated){
  print(element.index);
  print(element.value);
}
foo.enumerated.forEach((element){
  print(element.index);
  print(element.value);
}

Related issues

Lots of JavaScript developers rely on their overloadable map method which allows optionally accessing the index, so tutorials like this are created to shim this missing functionality https://fireship.io/snippets/dart-how-to-get-the-index-on-array-loop-map/

This is a common question on Stack Overflow

Screen Shot 2022-12-21 at 8 49 44 AM

Entire videos on YouTube are dedicated to helping people with this

https://www.youtube.com/watch?v=HLMzibT2Wms

There are some related issues here, although admittedly they are closed

dart-lang/sdk#49589
dart-lang/sdk#33965

@lukepighetti lukepighetti changed the title Add enumerated extension RFC: Add enumerated extension Dec 21, 2022
}
}

/// An enumerated element from an iterable
Copy link
Contributor

Choose a reason for hiding this comment

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

We're considering making MapEntry an inline class over a Record

I wonder if we'd prefer this also be a Record, and if so, if it would be worth waiting for that language feature to publish this API.

cc @lrhn

Copy link
Author

@lukepighetti lukepighetti Dec 22, 2022

Choose a reason for hiding this comment

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

I had this same thought, seems like a perfect use case for a Record, and waiting for that to land would make sense to me. What would be the best way for me to know when that language feature will be available for use in this PR?

Copy link
Author

Choose a reason for hiding this comment

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

Another option is to add this to the Iterable class in dart-lang/sdk (sdk/lib/core/iterable.dart), but I opened it up here because I figured it would be an easier conversation

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd definitely wait for Dart 3.0 and records and then add (maybe even to the platform libraries):

extension IterableIndexed<T> on Iterable<T> {
  Iterable<(int, T)> get indexed sync* { 
    var index = 0;
    for (var value in this) yield (index++, value);
  }
}

(I've already written it into the platform libraries in one of by branches. No promises that it'll land.)

@mosuem
Copy link
Contributor

mosuem commented Mar 12, 2024

Closing this as it is stale. Feel free to reopen in case you still want to land it!

@mosuem mosuem closed this Mar 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants