-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compose() does not work with generic functions #629
Comments
The problem is Can this line simply be reverted to Line 52 in 240f7b2
library(purrr)
foo <- function(...) UseMethod("foo")
foo.default <- function(...) message("foo")
purrr::compose(foo)()
#> foo
purrr::compose(foo,foo)()
#> foo
purrr::compose(foo,foo,foo)()
#> foo
body(purrr::compose(foo,foo,foo))
#> {
#> out <- {
#> UseMethod("foo")
#> }
#> fns <- list(function (...)
#> UseMethod("foo"), function (...)
#> UseMethod("foo"))
#> for (fn in fns) {
#> out <- fn(out)
#> }
#> out
#> }
# adding non-generic function works
purrr::compose(foo,foo,foo,function(...) NULL)()
#> foo
#> foo
#> foo
#> NULL Created on 2019-01-31 by the reprex package (v0.2.1.9000) |
@yutannihilation Good diagnostic. The goal was to have the same formals list as the original function, so we can have auto-completion of arguments etc. If I can't make it working with generic functions, I'll revert that feature. |
Thanks for the background, so we need to construct a function call by matching formals like this? wrap <- function(f) {
f_expr <- rlang::enexpr(f)
formal_args <- formals(f)
idx <- names(formal_args) != "..."
formal_args[idx] <- rlang::syms(names(formal_args))[idx]
body <- rlang::expr((!!f_expr)(!!!formal_args))
rlang::new_function(formals(f), body, rlang::caller_env())
}
wrap(mean)
#> function (x, ...)
#> mean(x = x, ... = )
wrap(dplyr::group_by)
#> function (.data, ..., add = FALSE, .drop = FALSE)
#> dplyr::group_by(.data = .data, ... = , add = add, .drop = .drop)
f <- mean
wrap(f)
#> function (x, ...)
#> f(x = x, ... = )
wrap(f)(1:10)
#> [1] 5.5 Created on 2019-02-26 by the reprex package (v0.2.1.9000) |
@yutannihilation This approach is not compatible with |
Ah, I see. |
In purrr 0.3.0
purrr::compose(t,t,identity)(1:3)
does not match
purrr::compose(t,t)(1:3)
In purrr 0.2.5 they match and I assume that this is the correct behavior
The text was updated successfully, but these errors were encountered: