Throwing StopIteration in array extras to stop the iteration

Domenic Denicola domenic at domenicdenicola.com
Mon Mar 4 12:21:37 PST 2013


From: es-discuss-bounces at mozilla.org [es-discuss-bounces at mozilla.org] on behalf of Rick Waldron [waldron.rick at gmail.com]

> Thanks, I've submitted an agenda item that includes _both_ find and findIndex.

Awesome!

One issue with `find` is what distinguishes

    find([1, 2, 3], x => isNaN(x)); // presumably `undefined`

from

    find([1, 2, undefined, 3], x => isNaN(x)); // also presumably `undefined`

i.e. how do you distinguish "found nothing" from "found `undefined`."

In C# they have `IEnumerable<T>.First` and `IEnumerable<T>.FirstOrDefault`; the former throws an error if nothing matching the predicate is found, whereas the latter returns `default(T)` (which is `null` for reference types, or `0` for numbers, etc.).

Food for thought.



More information about the es-discuss mailing list