Skip to content

Commit 8ab417d

Browse files
committed
Remove tail recursion
1 parent 940010d commit 8ab417d

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

R/nest.R

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -330,33 +330,35 @@ set_spaces <- function(spaces_after_prefix, force_one) {
330330
#' @return A nested parse table.
331331
#' @keywords internal
332332
nest_parse_data <- function(pd_flat) {
333-
if (all(pd_flat$parent <= 0L)) {
334-
return(pd_flat)
333+
repeat {
334+
if (all(pd_flat$parent <= 0L)) {
335+
return(pd_flat)
336+
}
337+
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L))
338+
split_data <- split(pd_flat, pd_flat$internal)
339+
340+
child <- split_data$`FALSE`
341+
internal <- split_data$`TRUE`
342+
343+
internal$internal_child <- internal$child
344+
internal$child <- NULL
345+
346+
child$parent_ <- child$parent
347+
348+
rhs <- nest_(child, "child", setdiff(names(child), "parent_"))
349+
350+
nested <- left_join(internal, rhs, by = c("id" = "parent_"))
351+
352+
children <- nested$child
353+
for (i in seq_along(children)) {
354+
new <- combine_children(children[[i]], nested$internal_child[[i]])
355+
# Work around is.null(new)
356+
children[i] <- list(new)
357+
}
358+
nested$child <- children
359+
nested$internal_child <- NULL
360+
pd_flat <- nested
335361
}
336-
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L))
337-
split_data <- split(pd_flat, pd_flat$internal)
338-
339-
child <- split_data$`FALSE`
340-
internal <- split_data$`TRUE`
341-
342-
internal$internal_child <- internal$child
343-
internal$child <- NULL
344-
345-
child$parent_ <- child$parent
346-
347-
rhs <- nest_(child, "child", setdiff(names(child), "parent_"))
348-
349-
nested <- left_join(internal, rhs, by = c("id" = "parent_"))
350-
351-
children <- nested$child
352-
for (i in seq_along(children)) {
353-
new <- combine_children(children[[i]], nested$internal_child[[i]])
354-
# Work around is.null(new)
355-
children[i] <- list(new)
356-
}
357-
nested$child <- children
358-
nested$internal_child <- NULL
359-
nest_parse_data(nested)
360362
}
361363

362364
#' Combine child and internal child

0 commit comments

Comments
 (0)