diff --git a/NEWS.md b/NEWS.md index 7a14011e..f2e8a460 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # purrr (development version) +* `as_mapper.default()` optimized by removing special named argument handling for primitive functions (@mtcarsalot, #1088). + * `list_flatten()` gains an `is_node` parameter taking a predicate function that determines whether an input element is a node or a leaf (@salim-b, #1179). * `in_parallel()` now accepts objects, including helper functions, supplied to `...` for all locally-defined functions (#1208). diff --git a/R/map-mapper.R b/R/map-mapper.R index e7ec6ce0..52f43515 100644 --- a/R/map-mapper.R +++ b/R/map-mapper.R @@ -43,18 +43,7 @@ as_mapper <- function(.f, ...) { #' @export as_mapper.default <- function(.f, ...) { - if (typeof(.f) %in% c("special", "builtin")) { - .f <- rlang::as_closure(.f) - - # Workaround until fixed in rlang - if (is_reference(fn_env(.f), base_env())) { - environment(.f) <- global_env() - } - - .f - } else { - rlang::as_function(.f) - } + rlang::as_function(.f) } #' @export diff --git a/tests/testthat/test-map-mapper.R b/tests/testthat/test-map-mapper.R index bc312f24..1ebba5fb 100644 --- a/tests/testthat/test-map-mapper.R +++ b/tests/testthat/test-map-mapper.R @@ -51,7 +51,7 @@ test_that("can supply length > 1 vectors", { # primitive functions -------------------------------------------------- test_that("primitive functions are wrapped", { - expect_identical(as_mapper(`-`)(.y = 10, .x = 5), -5) + expect_identical(as_mapper(`-`)(.y = 10, .x = 5), 5) # positional matching, not by name expect_identical(as_mapper(`c`)(1, 3, 5), c(1, 3, 5)) })