From cff48546711be8b282550db691bbd7ac26fe87d5 Mon Sep 17 00:00:00 2001 From: Jacob Bjerre Date: Fri, 19 Sep 2025 18:16:27 +0200 Subject: [PATCH 1/5] =?UTF-8?q?remove=20check=20for=20pimitives/builtin=20?= =?UTF-8?q?in=20=C2=B4as=5Fmapper.default()=C2=B4=20to=20improve=20perform?= =?UTF-8?q?ance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit change test to reflect Fixes #1088 --- R/map-mapper.R | 11 ----------- tests/testthat/test-map-mapper.R | 2 +- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/R/map-mapper.R b/R/map-mapper.R index e7ec6ce08..544fa8774 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) - } } #' @export diff --git a/tests/testthat/test-map-mapper.R b/tests/testthat/test-map-mapper.R index bc312f249..fad6332b6 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) expect_identical(as_mapper(`c`)(1, 3, 5), c(1, 3, 5)) }) From 986f20b5933f1eb1052c88711834e1958e0cc43c Mon Sep 17 00:00:00 2001 From: mtcarsalot <35042803+mtcarsalot@users.noreply.github.com> Date: Mon, 22 Sep 2025 12:11:03 +0200 Subject: [PATCH 2/5] Update tests/testthat/test-map-mapper.R Co-authored-by: Hadley Wickham --- tests/testthat/test-map-mapper.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-map-mapper.R b/tests/testthat/test-map-mapper.R index fad6332b6..cc0bca06c 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(`-`)(x, 10), -5) expect_identical(as_mapper(`c`)(1, 3, 5), c(1, 3, 5)) }) From b85dac0b7ff2883ad3e8ab044826be1ac4fbdf7b Mon Sep 17 00:00:00 2001 From: Jacob Bjerre Date: Mon, 22 Sep 2025 12:29:19 +0200 Subject: [PATCH 3/5] remove indentation --- R/map-mapper.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/map-mapper.R b/R/map-mapper.R index 544fa8774..52f435150 100644 --- a/R/map-mapper.R +++ b/R/map-mapper.R @@ -43,7 +43,7 @@ as_mapper <- function(.f, ...) { #' @export as_mapper.default <- function(.f, ...) { - rlang::as_function(.f) + rlang::as_function(.f) } #' @export From 1c23310005879fb5243ce4d489fe37e2fa42f652 Mon Sep 17 00:00:00 2001 From: Jacob Bjerre Date: Mon, 22 Sep 2025 12:30:51 +0200 Subject: [PATCH 4/5] add NEWS bullet and update test --- NEWS.md | 2 ++ tests/testthat/test-map-mapper.R | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 7a14011e5..c52203167 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # purrr (development version) +* Optimized `as_mapper.default()` by removing a check and handling of primitive function types. (#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/tests/testthat/test-map-mapper.R b/tests/testthat/test-map-mapper.R index cc0bca06c..fad6332b6 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(`-`)(x, 10), -5) + expect_identical(as_mapper(`-`)(.y = 10, .x = 5), 5) expect_identical(as_mapper(`c`)(1, 3, 5), c(1, 3, 5)) }) From e34ef167140cc7b4c6a828d16fb4dc6092d453f0 Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Mon, 22 Sep 2025 17:34:34 +0100 Subject: [PATCH 5/5] Edit news item and add comment to test --- NEWS.md | 2 +- tests/testthat/test-map-mapper.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index c52203167..f2e8a460b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # purrr (development version) -* Optimized `as_mapper.default()` by removing a check and handling of primitive function types. (#1088) +* `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). diff --git a/tests/testthat/test-map-mapper.R b/tests/testthat/test-map-mapper.R index fad6332b6..1ebba5fb3 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)) })