dart-lang / collection

The collection package for Dart contains a number of separate libraries with utility functions and classes that makes working with collections easier.

Home Page:https://pub.dev/packages/collection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need IterableExtension<E> with anyIndexed and everyIndexed

darkstarx opened this issue · comments

Could you implement anyIndexed and everyIndexed please?

extension IterableExt<E> on Iterable<E>
{
  bool everyIndexed(final bool Function(int i, E e) test)
  {
    var index = 0;
    for (final element in this) {
      if (!test(index, element)) return false;
    }
    return true;
  }

  bool anyIndexed(final bool Function(int index, E element) test)
  {
    var index = 0;
    for (final element in this) {
      if (test(index, element)) return true;
    }
    return false;
  }
}

There is now an indexed extension getter on Iterable in the platform libraries. You can do someIterable.indexed.any((iv) => indexTest(iv.$1) && valueTest(iv.$2)).

I hope to get better syntax for destructing f directly in the parameter list in the future.