avl
avl copied to clipboard
feature proposal: iterable interfaces
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,Setetc. 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)
}
I'd be happy to submit a PR with this!
Please do! Would that still be es5 compatible?
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 (...) {...}
}
That would be cool!
What API do you prefer?
treeis not iterabletree[Symbol.iterator]returns a keys iterable?tree[Symbol.iterator]returns an entries ([key, value]) iterable?
tree.keys()stays an array as before, addtree.keysIterable().- Change
tree.keys()from an array to an iterable. (breaking)
same for tree.values()
- tree[Symbol.iterator] returns an entries ([key, value]) iterable?
tree.keys()stays an array, as before, addtree.keysIterable()
right?
Okay, will implement this. A tree.valuesIterable() also makes sense, right?
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
I have implemented this at derhuerst/avl#iterables, but using ES6 generators. They are not supported in IE11, so we have two choices now:
- implement the iterators by hand using only ES5 syntax,
- or use Babel for transpiling or get bublé to transpile generators.
What do you prefer?
Opted for 1.