Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array.prototype.at compatibility? #148

Closed
Nemo64 opened this issue Aug 3, 2021 · 6 comments
Closed

Array.prototype.at compatibility? #148

Nemo64 opened this issue Aug 3, 2021 · 6 comments

Comments

@Nemo64
Copy link

Nemo64 commented Aug 3, 2021

I noticed that there is an .at method in the Array prototype in stage 3.
It seemed kind of useless to me for cases like [1, 2, 3].at(2).

But if iterators also implement it, then it'll suddenly make sense for compatibility between the Arrays and Iterators.

function getFirstStringItem(list: Iterable<any>): any {
    return list
        .filter(item => typeof item === 'string)
        .at(0); // would work with arrays and iterables
}

Implementing it with negative integers would require going though the entire iterator though.

Also, the iterator would be consumed by this wich might result in unexpected behavior:

function items(list: Iterable<any>) {
    return [
        list.at(0),
        list.at(1), // this would (probably) behave differently between arrays and iterators
    ]
}
@ljharb
Copy link
Member

ljharb commented Aug 3, 2021

Iterators don’t inherently have indexes, and the length can’t be known ahead of time - “at” would have to be like “take” and then “drop”, throwing away all the items up to that “index”.

@Nemo64
Copy link
Author

Nemo64 commented Aug 18, 2021

The negative index discussion is also in #131 so i'd say this depends on the outcome there.
And yes, .at(2) would be basically equivalent to .drop(2).next().value or .slice(2).next().value

@bakkot
Copy link
Collaborator

bakkot commented Oct 24, 2021

Apart from the negative index issue, I dislike using at for this because it has dramatically difference performance characteristics than it does for arrays and strings, so it will be easy to write quadratic code where identical code would be linear in another context.

@bathos
Copy link

bathos commented Oct 25, 2021

Assuming there's no proposal for "number of times next has been called" to become a built-in component of iterator state, it seems like iter.at(1) === iter.at(1) would have to be false (I mean, assuming the iterator doesn't just happen to yield the same value). The name at implies random access to me, not a state transition.

Rather than .at(2), .drop(2).next().value seems more like e.g. skipToValue(2). This is not an actual name suggestion, just saying that whatever name it did have should probably communicate something along those lines (that the index is relative to the current iterator state, etc).

@js-choi
Copy link

js-choi commented Oct 25, 2021

To add onto @bathos’s comment, Clojure uses nth versus get to distinguish linearly accessing the nth value from a sequence (consuming previous items in the process) from randomly accessing a value by a key from a vector or map object. As with Clojure sequences, nth might be a good name for linear access in JavaScript iterators too.

@michaelficarra
Copy link
Member

This doesn't provide much value over the proposed alternatives. We should not expand the proposal to include this method. If you would like, you can make a follow-on proposal, but I don't think this method will justify itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants