-
Notifications
You must be signed in to change notification settings - Fork 2
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
Want a way to iterate through a tree from right to left #107
Comments
Hi @xinxinw1! After a quick glance I think that you would need at least a Deep.prototype.reverseIter = function* () {
yield* reversed(this.right);
for (const node of this.middle.reverseIter()) yield* reversed(node);
yield* reversed(this.left);
}; Note that we iterate first on the right digit then on the left digit. To achieve the best performance I guess that digits and nodes could also get an implementation of Just a sanity check, following the definition of a tree:
In the sketched implementation of
Before this gets implemented here, you can try to write a function that achieve the same. You need to branch depending on the tree implementation: const reverseIter = ( tree ) => {
switch (tree.constructor) {
case Empty: return reverseIterEmpty();
case Single: return reverseIterSingle(tree);
case Deep: return reverseIterDeep(tree);
case Lazy: return reverseIterLazy(tree);
default: throw ...;
}
}; Then implement each case. You could even add digits and nodes to the switch once it works ( Can you try this? |
Note that it makes sense to implement this reverse iteration since monoid addition is associative. |
@xinxinw1 You can now use |
Hi @aureooms, I've been using your Finger tree javascript implementation in an online calculator that I'm building and so far it has been really good and easy to use. However today I found that I needed to iterate through the items of a tree from right to left and realized this was gonna be really difficult to do efficiently without changing your fingertree code.
Would it be possible to add something like an
Tree#reverseIter()
method or something like it to the tree API that'll efficiently iterate through the tree from right to left likeTree[Symbol.iterator]()
does from left to right?I'm guessing part of the implementation will probably look something like
but I'm not familiar enough with the code to be sure of the details.
The text was updated successfully, but these errors were encountered: