Skip to content

Commit 4d0668c

Browse files
committed
Optimize visiting code
1 parent 2fa7369 commit 4d0668c

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

R/visit.R

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ pre_visit <- function(pd_nested, funs) {
2020
if (is.null(pd_nested)) {
2121
return()
2222
}
23+
if (length(funs) == 0) {
24+
return(pd_nested)
25+
}
2326
pd_nested <- visit_one(pd_nested, funs)
2427

25-
pd_nested$child <- map(pd_nested$child, pre_visit, funs = funs)
28+
children <- pd_nested$child
29+
for (i in seq_along(children)) {
30+
child <- children[[i]]
31+
if (!is.null(child)) {
32+
children[[i]] <- pre_visit(child, funs)
33+
}
34+
}
35+
pd_nested$child <- children
2636
pd_nested
2737
}
2838

@@ -32,8 +42,19 @@ post_visit <- function(pd_nested, funs) {
3242
if (is.null(pd_nested)) {
3343
return()
3444
}
45+
if (length(funs) == 0) {
46+
return(pd_nested)
47+
}
48+
49+
children <- pd_nested$child
50+
for (i in seq_along(children)) {
51+
child <- children[[i]]
52+
if (!is.null(child)) {
53+
children[[i]] <- post_visit(child, funs)
54+
}
55+
}
56+
pd_nested$child <- children
3557

36-
pd_nested$child <- map(pd_nested$child, post_visit, funs = funs)
3758
visit_one(pd_nested, funs)
3859
}
3960

@@ -46,7 +67,10 @@ post_visit <- function(pd_nested, funs) {
4667
#' @family visitors
4768
#' @keywords internal
4869
visit_one <- function(pd_flat, funs) {
49-
Reduce(function(x, fun) fun(x), funs, init = pd_flat)
70+
for (f in funs) {
71+
pd_flat <- f(pd_flat)
72+
}
73+
pd_flat
5074
}
5175

5276
#' Propagate context to terminals

0 commit comments

Comments
 (0)