Skip to content

Commit b566017

Browse files
mtcarsalotjabj-jyskebank-dkhadleyshikokuchuo
authored
Remove check for primitive/builtin in as_mapper.default() to improve performance (#1218)
* remove check for pimitives/builtin in ´as_mapper.default()´ to improve performance change test to reflect Fixes #1088 * Update tests/testthat/test-map-mapper.R Co-authored-by: Hadley Wickham <hadley@posit.co> * remove indentation * add NEWS bullet and update test * Edit news item and add comment to test --------- Co-authored-by: Jacob Bjerre <jabj@jyskebank.dk> Co-authored-by: Hadley Wickham <hadley@posit.co> Co-authored-by: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com>
1 parent 820bd0c commit b566017

File tree

3 files changed

+4
-13
lines changed

3 files changed

+4
-13
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# purrr (development version)
22

3+
* `as_mapper.default()` optimized by removing special named argument handling for primitive functions (@mtcarsalot, #1088).
4+
35
* `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).
46

57
* `in_parallel()` now accepts objects, including helper functions, supplied to `...` for all locally-defined functions (#1208).

R/map-mapper.R

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,7 @@ as_mapper <- function(.f, ...) {
4343

4444
#' @export
4545
as_mapper.default <- function(.f, ...) {
46-
if (typeof(.f) %in% c("special", "builtin")) {
47-
.f <- rlang::as_closure(.f)
48-
49-
# Workaround until fixed in rlang
50-
if (is_reference(fn_env(.f), base_env())) {
51-
environment(.f) <- global_env()
52-
}
53-
54-
.f
55-
} else {
56-
rlang::as_function(.f)
57-
}
46+
rlang::as_function(.f)
5847
}
5948

6049
#' @export

tests/testthat/test-map-mapper.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test_that("can supply length > 1 vectors", {
5151
# primitive functions --------------------------------------------------
5252

5353
test_that("primitive functions are wrapped", {
54-
expect_identical(as_mapper(`-`)(.y = 10, .x = 5), -5)
54+
expect_identical(as_mapper(`-`)(.y = 10, .x = 5), 5) # positional matching, not by name
5555
expect_identical(as_mapper(`c`)(1, 3, 5), c(1, 3, 5))
5656
})
5757

0 commit comments

Comments
 (0)