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

feature proposal: iterable interfaces #23

Open
derhuerst opened this issue Sep 9, 2018 · 10 comments
Open

feature proposal: iterable interfaces #23

derhuerst opened this issue Sep 9, 2018 · 10 comments

Comments

@derhuerst
Copy link

derhuerst commented Sep 9, 2018

Using iterables to walk over a large amount of items/data has two advantages:

  • In contrast to computing a full array all items, items can be read from the tree on the fly. This prevents unnecessary graph walks.
  • Iterables are well-supported within ECMAScript and libs: String, Array, Map, Set etc. are iterable.

Examples of how iterables would be useful for this lib:

for (const key of tree.keys()) {
  await userRequestedAKey()
  console.log(key)
}

for (const node of tree.range('A', 'O')) {
  console.log(node.data)
}
@derhuerst
Copy link
Author

I'd be happy to submit a PR with this!

@w8r
Copy link
Owner

w8r commented Sep 13, 2018

Please do! Would that still be es5 compatible?

@derhuerst
Copy link
Author

derhuerst commented Sep 14, 2018

Would that still be es5 compatible?

Nope, Symbol.iterator is ES2015/ES6, so not supported in IE11.

But I think you can implement it in a backwards-compatible way:

if (Symbol && Symbol.iterator) {
  AVLTree.prototype[Symbol.iterator] = function (...) {...}
}

@w8r
Copy link
Owner

w8r commented Sep 14, 2018

That would be cool!

@derhuerst
Copy link
Author

derhuerst commented Sep 14, 2018

What API do you prefer?

  1. tree is not iterable
  2. tree[Symbol.iterator] returns a keys iterable?
  3. tree[Symbol.iterator] returns an entries ([key, value]) iterable?

  1. tree.keys() stays an array as before, add tree.keysIterable().
  2. Change tree.keys() from an array to an iterable. (breaking)

same for tree.values()

@w8r
Copy link
Owner

w8r commented Sep 14, 2018

  1. tree[Symbol.iterator] returns an entries ([key, value]) iterable?

  1. tree.keys() stays an array, as before, add tree.keysIterable()

right?

@derhuerst
Copy link
Author

Okay, will implement this. A tree.valuesIterable() also makes sense, right?

@w8r
Copy link
Owner

w8r commented Sep 14, 2018

yes, let's do it like that. Maybe I will change the API later. I will also add your changes to the splay-tree repo then

@derhuerst
Copy link
Author

derhuerst commented Sep 16, 2018

I have implemented this at derhuerst/avl#iterables, but using ES6 generators. They are not supported in IE11, so we have two choices now:

  1. implement the iterators by hand using only ES5 syntax,
  2. or use Babel for transpiling or get bublé to transpile generators.

What do you prefer?

@derhuerst
Copy link
Author

Opted for 1.

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