【发布时间】:2023-03-29 10:51:01
【问题描述】:
require(magrittr)
require(purrr)
is.out.same <- function(.call, ...) {
## Checks if args in .call will produce identical output in other functions
call <- substitute(.call) # Captures function call
f_names <- eval(substitute(alist(...))) # Makes list of f_names
map2(rep(list(call), length(f_names)), # Creates list of new function calls
f_names,
function(.x, .y, i) {.x[[1]] <- .y; return(.x)}
) %>%
map(eval) %>% # Evaluates function calls
map_lgl(identical, x = .call) %>% # Checks output of new calls against output of original call
all() # Returns TRUE if calls produce identical outputs
}
is.out.same(map(1:3, cumsum), lapply) # This works
map(1:3, cumsum) %>% # Is there any way to make this work?
is.out.same(lapply)
我的函数将函数调用作为参数。
有什么方法可以让我的函数可管道化?现在,问题是我调用的任何函数都将在管道之前进行评估。我唯一能想到的就是使用一个函数来“取消评估”这个值,但这似乎是不可能的。
【问题讨论】:
标签: r metaprogramming lazy-evaluation purrr magrittr