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

Allow for early termination of the visitor #18

Closed
LukasPietzschmann opened this issue Aug 30, 2024 · 3 comments · Fixed by #19
Closed

Allow for early termination of the visitor #18

LukasPietzschmann opened this issue Aug 30, 2024 · 3 comments · Fixed by #19
Assignees
Labels
enhancement New feature or request

Comments

@LukasPietzschmann
Copy link
Collaborator

When visiting nodes in the AST, it could be handy if the visitor would allow for an early exit. This could work as follows:

visit_nodes(ast, function(node) {
  if(some check) {
    # We got what we want, so we do not have to visit more nodes
    return(FALSE)
  }
  # return nothing (or TRUE) to visit more nodes
})

If you (@Ellpeck and @EagleoutIce) are fine with this small addition, I already have the code ready :)

@LukasPietzschmann LukasPietzschmann added the enhancement New feature or request label Aug 30, 2024
@LukasPietzschmann LukasPietzschmann self-assigned this Aug 30, 2024
@EagleoutIce
Copy link
Member

This mirrors flowR's behavior of allowing for an early exist, so please go ahead and open the PR :)

@LukasPietzschmann LukasPietzschmann linked a pull request Aug 30, 2024 that will close this issue
@LukasPietzschmann
Copy link
Collaborator Author

Maybe one small reiteration: I'm unsure of the right behavior when calling visit_nodes with a list of nodes. Should we instantly stop when the callback returns FALSE for a single node, or should we continue with the next node in the list?

@EagleoutIce
Copy link
Member

I think the "intuitive" thing would be to stop early. We define a sequential traversal order through the vector of nodes passed.
The R-Thing to do would be probably to return a vector greedly containing TRUE/FALSE for each node individually (i.e., vectorizing over the visitation order). But i think an early exit works perfectly fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants