diff --git a/NAMESPACE b/NAMESPACE index e754bc4a..bb906a65 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -60,6 +60,7 @@ export(imap_dfr) export(imap_int) export(imap_lgl) export(imap_raw) +export(imap_vec) export(imodify) export(insistently) export(invoke) diff --git a/NEWS.md b/NEWS.md index ee2cd0b6..b26c4487 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # purrr (development version) +* Added `imap_vec()` (#1084) * `list_transpose()` inspects all elements to determine the correct template if it's not provided by the user (#1128, @krlmlr). diff --git a/R/imap.R b/R/imap.R index fa1d9e52..ec1b01ff 100644 --- a/R/imap.R +++ b/R/imap.R @@ -56,6 +56,13 @@ imap_dbl <- function(.x, .f, ...) { map2_dbl(.x, vec_index(.x), .f, ...) } +#' @rdname imap +#' @export +imap_vec <- function(.x, .f, ...) { + .f <- as_mapper(.f, ...) + map2_vec(.x, vec_index(.x), .f, ...) +} + #' @export #' @rdname imap diff --git a/man/imap.Rd b/man/imap.Rd index ae081b01..b453d245 100644 --- a/man/imap.Rd +++ b/man/imap.Rd @@ -6,6 +6,7 @@ \alias{imap_chr} \alias{imap_int} \alias{imap_dbl} +\alias{imap_vec} \alias{iwalk} \title{Apply a function to each element of a vector, and its index} \usage{ @@ -19,6 +20,8 @@ imap_int(.x, .f, ...) imap_dbl(.x, .f, ...) +imap_vec(.x, .f, ...) + iwalk(.x, .f, ...) } \arguments{ diff --git a/tests/testthat/test-imap.R b/tests/testthat/test-imap.R index 79314d7e..bd1536e1 100644 --- a/tests/testthat/test-imap.R +++ b/tests/testthat/test-imap.R @@ -13,6 +13,7 @@ test_that("atomic vector imap works", { expect_length(imap_chr(x, paste), 3) expect_equal(imap_int(x, ~ .x + as.integer(.y)), x * 2) expect_equal(imap_dbl(x, ~ .x + as.numeric(.y)), x * 2) + expect_equal(imap_vec(x, ~ .x + as.numeric(.y)), x * 2) }) test_that("iwalk returns invisibly", {