Skip to content

Commit

Permalink
Improve the performance of as_mapper() (#899)
Browse files Browse the repository at this point in the history
Fixes #820
  • Loading branch information
hadley authored Aug 30, 2022
1 parent ccf8f8f commit 469f257
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

## Features and fixes

* `as_mapper()` is now around twice as fast when used with character,
integer, or list (#820).

* `vec_depth()` is now `pluck_depth()` and works with more types of input
(#818).

Expand Down
5 changes: 3 additions & 2 deletions R/as_mapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ find_extract_default <- function(.null, .default) {

plucker <- function(i, default) {
x <- NULL # supress global variables check NOTE
i <- as.list(i)

# Use metaprogramming to create function that prints nicely
new_function(
exprs(x = , ... = ),
expr(pluck(x, !!!i, .default = !!default)),
env = caller_env()
expr(pluck_raw(x, !!i, .default = !!default))
)
}

Expand Down
6 changes: 5 additions & 1 deletion R/pluck.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,19 @@
#' @export
pluck <- function(.x, ..., .default = NULL) {
check_dots_unnamed()
pluck_raw(.x, list2(...), .default = .default)
}

pluck_raw <- function(.x, index, .default = NULL) {
.Call(
pluck_impl,
x = .x,
index = list2(...),
index = index,
missing = .default,
strict = FALSE
)
}

#' @rdname pluck
#' @export
chuck <- function(.x, ...) {
Expand Down

0 comments on commit 469f257

Please sign in to comment.