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: [email protected] [[email protected]] on behalf
of Rick Waldron [[email protected]]
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
[email protected]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss