|
| 1 | +# nocov start - compat-purrr.R |
| 2 | +# Latest version: https://github.com/r-lib/rlang/blob/master/R/compat-purrr.R |
| 3 | + |
| 4 | +# This file provides a minimal shim to provide a purrr-like API on top of |
| 5 | +# base R functions. They are not drop-in replacements but allow a similar style |
| 6 | +# of programming. |
| 7 | +# |
| 8 | +# Changelog: |
| 9 | +# 2020-04-14: |
| 10 | +# * Removed `pluck*()` functions |
| 11 | +# * Removed `*_cpl()` functions |
| 12 | +# * Used `as_function()` to allow use of `~` |
| 13 | +# * Used `.` prefix for helpers |
| 14 | +# |
| 15 | +# 2021-05-21: |
| 16 | +# * Fixed "object `x` not found" error in `imap()` (@mgirlich) |
| 17 | + |
| 18 | +map <- function(.x, .f, ...) { |
| 19 | + .f <- as_function(.f, env = global_env()) |
| 20 | + lapply(.x, .f, ...) |
| 21 | +} |
| 22 | +walk <- function(.x, .f, ...) { |
| 23 | + map(.x, .f, ...) |
| 24 | + invisible(.x) |
| 25 | +} |
| 26 | + |
| 27 | +map_lgl <- function(.x, .f, ...) { |
| 28 | + .rlang_purrr_map_mold(.x, .f, logical(1), ...) |
| 29 | +} |
| 30 | +map_int <- function(.x, .f, ...) { |
| 31 | + .rlang_purrr_map_mold(.x, .f, integer(1), ...) |
| 32 | +} |
| 33 | +map_dbl <- function(.x, .f, ...) { |
| 34 | + .rlang_purrr_map_mold(.x, .f, double(1), ...) |
| 35 | +} |
| 36 | +map_chr <- function(.x, .f, ...) { |
| 37 | + .rlang_purrr_map_mold(.x, .f, character(1), ...) |
| 38 | +} |
| 39 | +.rlang_purrr_map_mold <- function(.x, .f, .mold, ...) { |
| 40 | + .f <- as_function(.f, env = global_env()) |
| 41 | + out <- vapply(.x, .f, .mold, ..., USE.NAMES = FALSE) |
| 42 | + names(out) <- names(.x) |
| 43 | + out |
| 44 | +} |
| 45 | + |
| 46 | +map2 <- function(.x, .y, .f, ...) { |
| 47 | + .f <- as_function(.f, env = global_env()) |
| 48 | + out <- mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE) |
| 49 | + if (length(out) == length(.x)) { |
| 50 | + set_names(out, names(.x)) |
| 51 | + } else { |
| 52 | + set_names(out, NULL) |
| 53 | + } |
| 54 | +} |
| 55 | +map2_lgl <- function(.x, .y, .f, ...) { |
| 56 | + as.vector(map2(.x, .y, .f, ...), "logical") |
| 57 | +} |
| 58 | +map2_int <- function(.x, .y, .f, ...) { |
| 59 | + as.vector(map2(.x, .y, .f, ...), "integer") |
| 60 | +} |
| 61 | +map2_dbl <- function(.x, .y, .f, ...) { |
| 62 | + as.vector(map2(.x, .y, .f, ...), "double") |
| 63 | +} |
| 64 | +map2_chr <- function(.x, .y, .f, ...) { |
| 65 | + as.vector(map2(.x, .y, .f, ...), "character") |
| 66 | +} |
| 67 | +imap <- function(.x, .f, ...) { |
| 68 | + map2(.x, names(.x) %||% seq_along(.x), .f, ...) |
| 69 | +} |
| 70 | + |
| 71 | +pmap <- function(.l, .f, ...) { |
| 72 | + .f <- as.function(.f) |
| 73 | + args <- .rlang_purrr_args_recycle(.l) |
| 74 | + do.call("mapply", c( |
| 75 | + FUN = list(quote(.f)), |
| 76 | + args, MoreArgs = quote(list(...)), |
| 77 | + SIMPLIFY = FALSE, USE.NAMES = FALSE |
| 78 | + )) |
| 79 | +} |
| 80 | +.rlang_purrr_args_recycle <- function(args) { |
| 81 | + lengths <- map_int(args, length) |
| 82 | + n <- max(lengths) |
| 83 | + |
| 84 | + stopifnot(all(lengths == 1L | lengths == n)) |
| 85 | + to_recycle <- lengths == 1L |
| 86 | + args[to_recycle] <- map(args[to_recycle], function(x) rep.int(x, n)) |
| 87 | + |
| 88 | + args |
| 89 | +} |
| 90 | + |
| 91 | +keep <- function(.x, .f, ...) { |
| 92 | + .x[.rlang_purrr_probe(.x, .f, ...)] |
| 93 | +} |
| 94 | +discard <- function(.x, .p, ...) { |
| 95 | + sel <- .rlang_purrr_probe(.x, .p, ...) |
| 96 | + .x[is.na(sel) | !sel] |
| 97 | +} |
| 98 | +map_if <- function(.x, .p, .f, ...) { |
| 99 | + matches <- .rlang_purrr_probe(.x, .p) |
| 100 | + .x[matches] <- map(.x[matches], .f, ...) |
| 101 | + .x |
| 102 | +} |
| 103 | +.rlang_purrr_probe <- function(.x, .p, ...) { |
| 104 | + if (is_logical(.p)) { |
| 105 | + stopifnot(length(.p) == length(.x)) |
| 106 | + .p |
| 107 | + } else { |
| 108 | + .p <- as_function(.p, env = global_env()) |
| 109 | + map_lgl(.x, .p, ...) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +compact <- function(.x) { |
| 114 | + Filter(length, .x) |
| 115 | +} |
| 116 | + |
| 117 | +transpose <- function(.l) { |
| 118 | + inner_names <- names(.l[[1]]) |
| 119 | + if (is.null(inner_names)) { |
| 120 | + fields <- seq_along(.l[[1]]) |
| 121 | + } else { |
| 122 | + fields <- set_names(inner_names) |
| 123 | + } |
| 124 | + |
| 125 | + map(fields, function(i) { |
| 126 | + map(.l, .subset2, i) |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +every <- function(.x, .p, ...) { |
| 131 | + .p <- as_function(.p, env = global_env()) |
| 132 | + |
| 133 | + for (i in seq_along(.x)) { |
| 134 | + if (!rlang::is_true(.p(.x[[i]], ...))) return(FALSE) |
| 135 | + } |
| 136 | + TRUE |
| 137 | +} |
| 138 | +some <- function(.x, .p, ...) { |
| 139 | + .p <- as_function(.p, env = global_env()) |
| 140 | + |
| 141 | + for (i in seq_along(.x)) { |
| 142 | + if (rlang::is_true(.p(.x[[i]], ...))) return(TRUE) |
| 143 | + } |
| 144 | + FALSE |
| 145 | +} |
| 146 | +negate <- function(.p) { |
| 147 | + .p <- as_function(.p, env = global_env()) |
| 148 | + function(...) !.p(...) |
| 149 | +} |
| 150 | + |
| 151 | +reduce <- function(.x, .f, ..., .init) { |
| 152 | + f <- function(x, y) .f(x, y, ...) |
| 153 | + Reduce(f, .x, init = .init) |
| 154 | +} |
| 155 | +reduce_right <- function(.x, .f, ..., .init) { |
| 156 | + f <- function(x, y) .f(y, x, ...) |
| 157 | + Reduce(f, .x, init = .init, right = TRUE) |
| 158 | +} |
| 159 | +accumulate <- function(.x, .f, ..., .init) { |
| 160 | + f <- function(x, y) .f(x, y, ...) |
| 161 | + Reduce(f, .x, init = .init, accumulate = TRUE) |
| 162 | +} |
| 163 | +accumulate_right <- function(.x, .f, ..., .init) { |
| 164 | + f <- function(x, y) .f(y, x, ...) |
| 165 | + Reduce(f, .x, init = .init, right = TRUE, accumulate = TRUE) |
| 166 | +} |
| 167 | + |
| 168 | +detect <- function(.x, .f, ..., .right = FALSE, .p = is_true) { |
| 169 | + .p <- as_function(.p, env = global_env()) |
| 170 | + .f <- as_function(.f, env = global_env()) |
| 171 | + |
| 172 | + for (i in .rlang_purrr_index(.x, .right)) { |
| 173 | + if (.p(.f(.x[[i]], ...))) { |
| 174 | + return(.x[[i]]) |
| 175 | + } |
| 176 | + } |
| 177 | + NULL |
| 178 | +} |
| 179 | +detect_index <- function(.x, .f, ..., .right = FALSE, .p = is_true) { |
| 180 | + .p <- as_function(.p, env = global_env()) |
| 181 | + .f <- as_function(.f, env = global_env()) |
| 182 | + |
| 183 | + for (i in .rlang_purrr_index(.x, .right)) { |
| 184 | + if (.p(.f(.x[[i]], ...))) { |
| 185 | + return(i) |
| 186 | + } |
| 187 | + } |
| 188 | + 0L |
| 189 | +} |
| 190 | +.rlang_purrr_index <- function(x, right = FALSE) { |
| 191 | + idx <- seq_along(x) |
| 192 | + if (right) { |
| 193 | + idx <- rev(idx) |
| 194 | + } |
| 195 | + idx |
| 196 | +} |
| 197 | + |
| 198 | +# nocov end |
0 commit comments