diff --git a/R/visit.R b/R/visit.R index 7c1eb37..d30ccb7 100644 --- a/R/visit.R +++ b/R/visit.R @@ -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) + } } } } @@ -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)) { @@ -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) diff --git a/man/visit_node.Rd b/man/visit_node.Rd index eb15cc5..cd7bcbe 100644 --- a/man/visit_node.Rd +++ b/man/visit_node.Rd @@ -11,6 +11,9 @@ visit_node(node, callback) \item{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.} } +\value{ +FALSE if the visitor was stopped by the callback. +} \description{ Visits the given node and all of their children, invoking the callback for each visited node } diff --git a/man/visit_nodes.Rd b/man/visit_nodes.Rd index edf553e..37bdc86 100644 --- a/man/visit_nodes.Rd +++ b/man/visit_nodes.Rd @@ -11,6 +11,9 @@ visit_nodes(nodes, callback) \item{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.} } +\value{ +FALSE if the visitor was stopped by the callback. +} \description{ Visits the set of nodes and all of their children, invoking the callback for each visited node }