Throwing StopIteration in array extras to stop the iteration

Brendan Eich brendan at mozilla.com
Mon Mar 4 12:28:44 PST 2013


One idea we've discussed: allow the sentinel that is OOB with respect to 
the domain (element type) be an optional second parameter:

   const K = Symbol("not found");
   console.log( [1, 2, 3].find(x => isNaN(x), K) ); // K, logs as "not 
found"
   console.log( [1, 2, , 3].find(x => isNaN(x)) );  // undefined

/be

P.S. Don't use isNaN! I know, it served your example's purpose.

Domenic Denicola wrote:
> 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.
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list