Skip to content

Commit

Permalink
Stop visit_nodes early when visit_node also stopped early
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPietzschmann committed Aug 30, 2024
1 parent 707c9df commit bd28342
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions R/visit.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
#' @param nodes The list or array of nodes to visit
#' @param callback The callback function to invoke for each node. The callback should return FALSE to stop visiting the children of the node, or anything else to continue.
#'
#' @return FALSE if the visitor was stopped by the callback.
#'
#' @export
visit_nodes <- function(nodes, callback) {
if (!is.null(nodes)) {
for (node in nodes) {
visit_node(node, callback)
res <- visit_node(node, callback)
if (isFALSE(res)) {
return(FALSE)
}
}
}
}
Expand All @@ -17,6 +22,8 @@ visit_nodes <- function(nodes, callback) {
#' @param node The node to visit
#' @param callback The callback function to invoke for each node. The callback should return FALSE to stop visiting the children of the node, or anything else to continue.
#'
#' @return FALSE if the visitor was stopped by the callback.
#'
#' @export
visit_node <- function(node, callback) {
if (is.null(node)) {
Expand All @@ -26,7 +33,7 @@ visit_node <- function(node, callback) {
res <- callback(node)
# Exit early if the callback returns FALSE
if (isFALSE(res)) {
return()
return(FALSE)
}

# same logic as the builtin visitor (while explicitly specifying if an entry is a single node or a list)
Expand Down
3 changes: 3 additions & 0 deletions man/visit_node.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/visit_nodes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd28342

Please sign in to comment.