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

Introduce 'recur' callback #58

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Introduce 'recur' callback #58

wants to merge 5 commits into from

Conversation

ghost23
Copy link

@ghost23 ghost23 commented Sep 27, 2015

Hi,

for my projects, I felt the need for an in-order callback during tree traversal.
(e.g. https://en.wikipedia.org/wiki/Tree_traversal -> in-order)

This pull request introduces a 'recur' callback beside 'enter' and 'leave'. recur is called with the parent element between the traversal of its children.

Think of this simple graph:

  Parent
    /\
   /  \
  A    B

With recur, the overall sequence of traversal will be: Enter Parent, Enter A, Leave A, Recur Parent, Enter B, Leave B, Leave Parent. This, beside other usecases, can be useful to check, wether a node is actually a grandchild or a sibling of another node, which can be hard to detect with enter and leave alone.

Recur here only supports BREAK. Also, within replace, recur does not support replacing or removing, only BREAK.

It is just a little addition, but perhaps you find it useful, too.

Cheers,
Sven

@Constellation
Copy link
Member

I think https://github.com/estools/esrecurse is useful for this use case. How about this?

@ghost23
Copy link
Author

ghost23 commented Oct 19, 2015

I am not sure I understand, what node.left node.right are supposed to mean in esrecurse.

Anyway, I'd rather have the functionality in estraverse than having to use a different lib for special use cases.

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

Successfully merging this pull request may close these issues.

2 participants