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

Can next()/prev() als be included? #14

Open
trusktr opened this issue Nov 21, 2024 · 1 comment
Open

Can next()/prev() als be included? #14

trusktr opened this issue Nov 21, 2024 · 1 comment

Comments

@trusktr
Copy link

trusktr commented Nov 21, 2024

I would like to iterate a Set, for example, in either direction, and change direction during iteration. This could, for example, replace a (maybe circular) linked list.

More thoughts here:

https://es.discourse.group/t/next-prev-for-non-async-iterators/2224

Motivating example:

This example is obviously not very well thought out (next() returns {value: ___, ...}), but it shows what the end-user desire is:

const set = new Set(...) // maybe this comes from somewhere else (imported, component prop, etc)

const vals = set.values()
let selected = vals.next()

// ... some reactive framework template ...
return <div>
  <p>name: {selected.name}</p>
  <button onclick={() => selected = vals.next()}>next</button>
  <button onclick={() => selected = vals.prev()}>prev</button>
</div>
@ljharb
Copy link
Member

ljharb commented Nov 21, 2024

While this definitely works for an iterator over a finite collection (Set, Map, Array, String), in general it wouldn't work - in particular for most things that use generators or custom iterators. would you test for the presence of a prev method to distinguish?

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

2 participants