Array findLast() and findLastIndex()

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Array findLast() and findLastIndex()

Sebastian Malton
This function would be like find but would iterate from back to front. We already have find/findIndex but unlike indexOf they don't have a last counterpart.

Sebastian 


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array findLast() and findLastIndex()

Jordan Harband
indexOf and reduce are the only iteration methods that have "reverse" versions - I think to add any others, a clear case would have to be made that *all* of them shouldn't get the same treatment.

What's your use case?

On Fri, Aug 4, 2017 at 11:19 AM, Sebastian Malton <[hidden email]> wrote:
This function would be like find but would iterate from back to front. We already have find/findIndex but unlike indexOf they don't have a last counterpart.

Sebastian 


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array findLast() and findLastIndex()

Sebastian Malton
I don't specifically have an exact use case but I can definitely think of some use cases for them. However, if these sorts of functions also work for array iterators then a reverse entries functions should suffice.

Sebastian 

Sent: August 4, 2017 2:26 PM
Subject: Re: Array findLast() and findLastIndex()

indexOf and reduce are the only iteration methods that have "reverse" versions - I think to add any others, a clear case would have to be made that *all* of them shouldn't get the same treatment.

What's your use case?

On Fri, Aug 4, 2017 at 11:19 AM, Sebastian Malton <[hidden email]> wrote:
This function would be like find but would iterate from back to front. We already have find/findIndex but unlike indexOf they don't have a last counterpart.

Sebastian 


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Array findLast() and findLastIndex()

kdex
I'm thinking that this may have performance benefits. The naïve approach would
be something like:

```js
function findLast(array, fn) {
        return array.reverse().find(fn);
}
```

Note that this requires a reversed copy of `array` to reside in memory before
`find` can be run, which is not a particularly useful thing to do when all you
want is to start the search from another direction.

Instead, you would have to do something like:

```js
function findLast(array, predicate) {
        for (let i = array.length - 1; i >= 0; --i) {
                const x = array[i];
                if (predicate(x)) {
                        return x;
                }
        }
}
```

which does the job, I guess. Apart from performance considerations, I would
find it practical if all methods that work in *some* direction had a
counterpart for the other direction, c.f. `trim{Start,End}`, `pad{Start,End}`,
`reduce{,Right}`. It would be consistent, would it not?

On Friday, August 4, 2017 8:35:52 PM CEST Sebastian Malton wrote:

> I don't specifically have an exact use case but I can definitely think of
> some use cases for them. However, if these sorts of functions also work for
> array iterators then a reverse entries functions should suffice.
>
> Sebastian
>
> From:  [hidden email]
> Sent: August 4, 2017 2:26 PM
> To:  [hidden email]
> Cc: [hidden email]
> Subject: Re: Array findLast() and findLastIndex()
>
> indexOf and reduce are the only iteration methods that have "reverse"
> versions - I think to add any others, a clear case would have to be made
> that *all* of them shouldn't get the same treatment.
>
> What's your use case?
>
> On Fri, Aug 4, 2017 at 11:19 AM, Sebastian Malton <[hidden email]>
> wrote: This function would be like find but would iterate from back to
> front. We already have find/findIndex but unlike indexOf they don't have a
> last counterpart.
>
> Sebastian
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Array findLast() and findLastIndex()

Sebastian Malton
After some research iterators cannot be used because of lack of functions on them. However, for brevity and efficiency these sorts of functions should still exist.

Sebastian


  Original Message  
From: [hidden email]
Sent: August 4, 2017 3:10 PM
To: [hidden email]
Subject: Re: Array findLast() and findLastIndex()

I'm thinking that this may have performance benefits. The naïve approach would
be something like:

```js
function findLast(array, fn) {
return array.reverse().find(fn);
}
```

Note that this requires a reversed copy of `array` to reside in memory before
`find` can be run, which is not a particularly useful thing to do when all you
want is to start the search from another direction.

Instead, you would have to do something like:

```js
function findLast(array, predicate) {
for (let i = array.length - 1; i >= 0; --i) {
const x = array[i];
if (predicate(x)) {
return x;
}
}
}
```

which does the job, I guess. Apart from performance considerations, I would
find it practical if all methods that work in *some* direction had a
counterpart for the other direction, c.f. `trim{Start,End}`, `pad{Start,End}`,
`reduce{,Right}`. It would be consistent, would it not?

On Friday, August 4, 2017 8:35:52 PM CEST Sebastian Malton wrote:

> I don't specifically have an exact use case but I can definitely think of
> some use cases for them. However, if these sorts of functions also work for
> array iterators then a reverse entries functions should suffice.
>
> Sebastian
>
> From:  [hidden email]
> Sent: August 4, 2017 2:26 PM
> To:  [hidden email]
> Cc: [hidden email]
> Subject: Re: Array findLast() and findLastIndex()
>
> indexOf and reduce are the only iteration methods that have "reverse"
> versions - I think to add any others, a clear case would have to be made
> that *all* of them shouldn't get the same treatment.
>
> What's your use case?
>
> On Fri, Aug 4, 2017 at 11:19 AM, Sebastian Malton <[hidden email]>
> wrote: This function would be like find but would iterate from back to
> front. We already have find/findIndex but unlike indexOf they don't have a
> last counterpart.
>
> Sebastian
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss