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

support ES6 iterators #12

Open
haberman opened this issue Jun 14, 2014 · 6 comments
Open

support ES6 iterators #12

haberman opened this issue Jun 14, 2014 · 6 comments

Comments

@haberman
Copy link

The ES6 iterator protocol is a bit different than what js_bintrees implements. Any chance js_bintrees could add support for this?

This would allow code like this in ES6:

function logRBTree(tree) {
  for (item of tree.iterator()) {
    console.log(item);
  }
}

Unfortunately this would be a backward-incompatible change. The only two ideas that spring to mind are:

  1. having some global setting for which iterator style you vend.
  2. having two sets of methods (ie. tree.es6Iterator(), tree.es6FindIter(), etc).

Thanks for writing js_bintrees!

@haberman
Copy link
Author

(btw, I'm not actually using ES6 yet, but I'm writing an iterator-heavy app and trying to standardize on ES6 style. For now I'll just write wrappers around RBTree.)

@vadimg
Copy link
Owner

vadimg commented Jun 16, 2014

Yeah, the backwards-incompatibility of ES6 iterators with mine is a bit unfortunate. I'll have to think for a bit about how to best solve this. For now, it's not really an issue because ES6 hasn't even been standardized yet. Thanks for the heads up, though!

@sbking
Copy link

sbking commented Mar 24, 2015

It's a bit of an annoyance because many engines already support iterators/generators and many ES6 features can be compiled to ES5, but generators allow a simple solution that works well enough:

function* iterateTree(tree) {
  let it=tree.iterator(), item;
  while((item = it.next()) !== null) {
    yield item;
  }
}

for (let item of iterateTree(tree)) {
  // do stuff with item
}

@haberman
Copy link
Author

haberman commented Jul 9, 2015

Just wanted to mention that ES6 is complete now, with a standardized iterator interface!

@vadimg
Copy link
Owner

vadimg commented Jul 24, 2015

Ok, here's the plan:

  • Bump up the version to 2.0.0
  • By default, iterator() will work the ES6 way
  • There will be a global compatibility flag you can set to make iterator() work the old way. This flag will have an option:
    • By default, it will print a compatibility warning to console.log whenever iterator() is called. This way you can make sure that you have changed all instances of iterator() to reversibleIterator() before you disable compatibility mode.
    • If you don't care to ever update your code, you can disable this warning.
  • If you want to use the old iterator, it will be available as reversibleIterator()

What do you guys think?

@haberman
Copy link
Author

What about findIter(), lowerBound(), and upperBound()? Would there be two versions of those methods?

I do see the value of having old-style iterators also, because they are more flexible. A single old-style iterator can go forwards or backwards or even switch in the middle of iteration.

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

3 participants