Throwing StopIteration in array extras to stop the iteration

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Mar 4 14:53:27 PST 2013


Thanks a lot.

On find(), I believe it will always be ambiguous, compared to findIndex,
for the simple reason that an Array could contain undefined too [1,
undefined, 2] ... probably an edge case not worth the consideration, but
this looks like a legit code to me:

[1, undefined, 2].find(function (item) {
  return typeof item === 'undefined';
}) === undefined;

who knows if that was the undefined item, or the missing result ... not
sure how to solve that ... just thinking about something like:

var found = /* same code */

if (Array.itemFound) {
  // use found
} else {
  arr.push(createSomethingCool());
}

Array.itemFound would be "as ugly as RegExp.$N" and true until next
[].find() call ... plus read only for the user.
Well, this might work ... I guess

br






On Mon, Mar 4, 2013 at 12:11 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

>
>
>
> On Mon, Mar 4, 2013 at 2:37 PM, Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
>
>> this is what 've wrote as prototype at the end of the post. findIndex
>> makes sense to me and is better, in term of manipulation, than finding just
>> an element.
>>
>> (function(AP){
>> AP.findIndex || (
>>   AP.findIndex = function(fn, self) {
>>     var $i = -1;
>>     AP.some.call(this, function(v, i, a) {
>>       if (fn.call(this, v, i, a)) {
>>         $i = i;
>>         return true;
>>       }
>>     }, self);
>>     return $i;
>>   };
>> );
>> }(Array.prototype));
>>
>> As you can see, and for consistency reasons, it returns -1 when the
>> condition is not found.
>>
>> Then we might discuss how useful would be a startFromIndex property but I
>> believe the optional context is more useful than that (and indeed there are
>> very few cases I needed that with Array#indexOf)
>>
>
>
> Thanks, I've submitted an agenda item that includes _both_ find and
> findIndex.
>
> Rick
>
> ps. @Andrea I also squeezed "unique" into the agenda addition ;)
>
>
>>
>> br
>>
>>
>> On Mon, Mar 4, 2013 at 11:32 AM, Rick Waldron <waldron.rick at gmail.com>wrote:
>>
>>>
>>>
>>>
>>> On Mon, Mar 4, 2013 at 11:29 AM, Jeff Walden <jwalden+es at mit.edu> wrote:
>>>
>>>> On 03/03/2013 06:49 PM, Rick Waldron wrote:
>>>> > Is this +1 to findIndex?
>>>>
>>>> Not that I much care between the two, just making sure another
>>>> reasonable name is considered, but I'm not sure why it wouldn't be named
>>>> "find" rather than "findIndex".
>>>
>>>
>>> Because "find' isn't "findIndex"—find would return the element,
>>> findIndex would return the index.
>>>
>>> Rick
>>>
>>>
>>>> The index seems like the only bit you'd reasonably be looking to find.
>>>>  (Well, maybe existence, but I'd expect a name like "contains" for that, or
>>>> just indexOf !== -1.)
>>>>
>>>> Jeff
>>>>
>>>
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130304/eafc05f2/attachment-0001.html>


More information about the es-discuss mailing list