Debian Codegraph @

R/compat-purrr.R

raw
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 # 2021-12-15:
19 # * `transpose()` now supports empty lists.
20
21 map <- function(.x, .f, ...) {
22 .f <- as_function(.f, env = global_env())
23 lapply(.x, .f, ...)
24 }
25 walk <- function(.x, .f, ...) {
26 map(.x, .f, ...)
27 invisible(.x)
28 }
29
30 map_lgl <- function(.x, .f, ...) {
31 .rlang_purrr_map_mold(.x, .f, logical(1), ...)
32 }
33 map_int <- function(.x, .f, ...) {
34 .rlang_purrr_map_mold(.x, .f, integer(1), ...)
35 }
36 map_dbl <- function(.x, .f, ...) {
37 .rlang_purrr_map_mold(.x, .f, double(1), ...)
38 }
39 map_chr <- function(.x, .f, ...) {
40 .rlang_purrr_map_mold(.x, .f, character(1), ...)
41 }
42 .rlang_purrr_map_mold <- function(.x, .f, .mold, ...) {
43 .f <- as_function(.f, env = global_env())
44 out <- vapply(.x, .f, .mold, ..., USE.NAMES = FALSE)
45 names(out) <- names(.x)
46 out
47 }
48
49 map2 <- function(.x, .y, .f, ...) {
50 .f <- as_function(.f, env = global_env())
51 out <- mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE)
52 if (length(out) == length(.x)) {
53 set_names(out, names(.x))
54 } else {
55 set_names(out, NULL)
56 }
57 }
58 map2_lgl <- function(.x, .y, .f, ...) {
59 as.vector(map2(.x, .y, .f, ...), "logical")
60 }
61 map2_int <- function(.x, .y, .f, ...) {
62 as.vector(map2(.x, .y, .f, ...), "integer")
63 }
64 map2_dbl <- function(.x, .y, .f, ...) {
65 as.vector(map2(.x, .y, .f, ...), "double")
66 }
67 map2_chr <- function(.x, .y, .f, ...) {
68 as.vector(map2(.x, .y, .f, ...), "character")
69 }
70 imap <- function(.x, .f, ...) {
71 map2(.x, names(.x) %||% seq_along(.x), .f, ...)
72 }
73
74 pmap <- function(.l, .f, ...) {
75 .f <- as.function(.f)
76 args <- .rlang_purrr_args_recycle(.l)
77 do.call("mapply", c(
78 FUN = list(quote(.f)),
79 args, MoreArgs = quote(list(...)),
80 SIMPLIFY = FALSE, USE.NAMES = FALSE
81 ))
82 }
83 .rlang_purrr_args_recycle <- function(args) {
84 lengths <- map_int(args, length)
85 n <- max(lengths)
86
87 stopifnot(all(lengths == 1L | lengths == n))
88 to_recycle <- lengths == 1L
89 args[to_recycle] <- map(args[to_recycle], function(x) rep.int(x, n))
90
91 args
92 }
93
94 keep <- function(.x, .f, ...) {
95 .x[.rlang_purrr_probe(.x, .f, ...)]
96 }
97 discard <- function(.x, .p, ...) {
98 sel <- .rlang_purrr_probe(.x, .p, ...)
99 .x[is.na(sel) | !sel]
100 }
101 map_if <- function(.x, .p, .f, ...) {
102 matches <- .rlang_purrr_probe(.x, .p)
103 .x[matches] <- map(.x[matches], .f, ...)
104 .x
105 }
106 .rlang_purrr_probe <- function(.x, .p, ...) {
107 if (is_logical(.p)) {
108 stopifnot(length(.p) == length(.x))
109 .p
110 } else {
111 .p <- as_function(.p, env = global_env())
112 map_lgl(.x, .p, ...)
113 }
114 }
115
116 compact <- function(.x) {
117 Filter(length, .x)
118 }
119
120 transpose <- function(.l) {
121 if (!length(.l)) {
122 return(.l)
123 }
124 inner_names <- names(.l[[1]])
125 if (is.null(inner_names)) {
126 fields <- seq_along(.l[[1]])
127 } else {
128 fields <- set_names(inner_names)
129 }
130
131 map(fields, function(i) {
132 map(.l, .subset2, i)
133 })
134 }
135
136 every <- function(.x, .p, ...) {
137 .p <- as_function(.p, env = global_env())
138
139 for (i in seq_along(.x)) {
140 if (!rlang::is_true(.p(.x[[i]], ...))) return(FALSE)
141 }
142 TRUE
143 }
144 some <- function(.x, .p, ...) {
145 .p <- as_function(.p, env = global_env())
146
147 for (i in seq_along(.x)) {
148 if (rlang::is_true(.p(.x[[i]], ...))) return(TRUE)
149 }
150 FALSE
151 }
152 negate <- function(.p) {
153 .p <- as_function(.p, env = global_env())
154 function(...) !.p(...)
155 }
156
157 reduce <- function(.x, .f, ..., .init) {
158 f <- function(x, y) .f(x, y, ...)
159 Reduce(f, .x, init = .init)
160 }
161 reduce_right <- function(.x, .f, ..., .init) {
162 f <- function(x, y) .f(y, x, ...)
163 Reduce(f, .x, init = .init, right = TRUE)
164 }
165 accumulate <- function(.x, .f, ..., .init) {
166 f <- function(x, y) .f(x, y, ...)
167 Reduce(f, .x, init = .init, accumulate = TRUE)
168 }
169 accumulate_right <- function(.x, .f, ..., .init) {
170 f <- function(x, y) .f(y, x, ...)
171 Reduce(f, .x, init = .init, right = TRUE, accumulate = TRUE)
172 }
173
174 detect <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
175 .p <- as_function(.p, env = global_env())
176 .f <- as_function(.f, env = global_env())
177
178 for (i in .rlang_purrr_index(.x, .right)) {
179 if (.p(.f(.x[[i]], ...))) {
180 return(.x[[i]])
181 }
182 }
183 NULL
184 }
185 detect_index <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
186 .p <- as_function(.p, env = global_env())
187 .f <- as_function(.f, env = global_env())
188
189 for (i in .rlang_purrr_index(.x, .right)) {
190 if (.p(.f(.x[[i]], ...))) {
191 return(i)
192 }
193 }
194 0L
195 }
196 .rlang_purrr_index <- function(x, right = FALSE) {
197 idx <- seq_along(x)
198 if (right) {
199 idx <- rev(idx)
200 }
201 idx
202 }
203
204 # nocov end