collection icon indicating copy to clipboard operation
collection copied to clipboard

`partition` suggestion

Open Nhowka opened this issue 4 years ago • 1 comments

I found the package after reinventing some functions it had and it almost fulfilled all my needs. Thanks for the great work!

The one function I missed (coming from an F# background) was the partition. There, partition takes a list and return a tuple, where the first list are all elements that passed the test and the second, all the elements that didn't.

As dart has no support for tuples, I thought about using a combine function that uses both Iterables and create a new object from them. Here is some naive code of what it could look like inside an extension on Iterable<T>:

  R partition<R>(bool Function(T element) test,
          R Function(Iterable<T> passed, Iterable<T> rejected) combine) =>
      combine(this.where(test), this.whereNot(test));

I plan to use it to sort some mixed data from the server, where some of it is already fully loaded while the other part needs further loading. Then I can queue the loading of just the second list while showing the first.

Not sure if this is too niche of a case to be on the library. What do you think?

Nhowka avatar May 12 '21 16:05 Nhowka

I wrote this a few days ago and I would love to have this added to the library.

JulianBissekkou avatar Jul 06 '21 17:07 JulianBissekkou