On Tue, 25 Jul 2017 at 12:09 T.J. Crowder <[email protected]> wrote:
> On Tue, Jul 25, 2017 at 5:06 AM, Naveen Chawla <[email protected]> > wrote: > >> I would not overload `each` with any early termination, I would probably >> have an `Array.prototype.takeWhile` method for that (which returns a new >> array taking elements while the return value of the predicate is truthy). >> > > This is for when you don't want to create a new array, for either semantic > or memory pressure reasons. > > -- T.J. Crowder > For that I would have `eachWhile` which returns the original array but only iterates while the return value of the supplied callback is truthy. The difference with `takeWhile` (which should be added to ES regardless, along with `skipWhile`) is that in chaining calls `takeWhile` provides a potentially smaller array for further processing, whereas `eachWhile` doesn't. (`eachSkipWhile` could be the "skipWhile" counterpart to `eachWhile`). I would leave `each` as a contract: always iterates through every value, always returns the original array. For me this is better for readability. If these chained calls `map`, `each` etc. can be integrated with async iterators (as RxJs attempts with Observables) then some really elegant stuff can happen!
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

