Debian Codegraph @

R/compat-purrr.R

raw
1 # nocov start - compat-purrr (last updated: rlang 0.3.2.9000)
2
3 # This file serves as a reference for compatibility functions for
4 # purrr. They are not drop-in replacements but allow a similar style
5 # of programming. This is useful in cases where purrr is too heavy a
6 # package to depend on. Please find the most recent version in rlang's
7 # repository.
8
9 map <- function(.x, .f, ...) {
10 lapply(.x, .f, ...)
11 }
12 map_mold <- function(.x, .f, .mold, ...) {
13 out <- vapply(.x, .f, .mold, ..., USE.NAMES = FALSE)
14 names(out) <- names(.x)
15 out
16 }
17 map_lgl <- function(.x, .f, ...) {
18 map_mold(.x, .f, logical(1), ...)
19 }
20 map_int <- function(.x, .f, ...) {
21 map_mold(.x, .f, integer(1), ...)
22 }
23 map_dbl <- function(.x, .f, ...) {
24 map_mold(.x, .f, double(1), ...)
25 }
26 map_chr <- function(.x, .f, ...) {
27 map_mold(.x, .f, character(1), ...)
28 }
29 map_cpl <- function(.x, .f, ...) {
30 map_mold(.x, .f, complex(1), ...)
31 }
32
33 walk <- function(.x, .f, ...) {
34 map(.x, .f, ...)
35 invisible(.x)
36 }
37
38 pluck <- function(.x, .f) {
39 map(.x, `[[`, .f)
40 }
41 pluck_lgl <- function(.x, .f) {
42 map_lgl(.x, `[[`, .f)
43 }
44 pluck_int <- function(.x, .f) {
45 map_int(.x, `[[`, .f)
46 }
47 pluck_dbl <- function(.x, .f) {
48 map_dbl(.x, `[[`, .f)
49 }
50 pluck_chr <- function(.x, .f) {
51 map_chr(.x, `[[`, .f)
52 }
53 pluck_cpl <- function(.x, .f) {
54 map_cpl(.x, `[[`, .f)
55 }
56
57 map2 <- function(.x, .y, .f, ...) {
58 out <- mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE)
59 if (length(out) == length(.x)) {
60 set_names(out, names(.x))
61 } else {
62 set_names(out, NULL)
63 }
64 }
65 map2_lgl <- function(.x, .y, .f, ...) {
66 as.vector(map2(.x, .y, .f, ...), "logical")
67 }
68 map2_int <- function(.x, .y, .f, ...) {
69 as.vector(map2(.x, .y, .f, ...), "integer")
70 }
71 map2_dbl <- function(.x, .y, .f, ...) {
72 as.vector(map2(.x, .y, .f, ...), "double")
73 }
74 map2_chr <- function(.x, .y, .f, ...) {
75 as.vector(map2(.x, .y, .f, ...), "character")
76 }
77 map2_cpl <- function(.x, .y, .f, ...) {
78 as.vector(map2(.x, .y, .f, ...), "complex")
79 }
80
81 args_recycle <- function(args) {
82 lengths <- map_int(args, length)
83 n <- max(lengths)
84
85 stopifnot(all(lengths == 1L | lengths == n))
86 to_recycle <- lengths == 1L
87 args[to_recycle] <- map(args[to_recycle], function(x) rep.int(x, n))
88
89 args
90 }
91 pmap <- function(.l, .f, ...) {
92 args <- args_recycle(.l)
93 do.call("mapply", c(
94 FUN = list(quote(.f)),
95 args, MoreArgs = quote(list(...)),
96 SIMPLIFY = FALSE, USE.NAMES = FALSE
97 ))
98 }
99
100 probe <- function(.x, .p, ...) {
101 if (is_logical(.p)) {
102 stopifnot(length(.p) == length(.x))
103 .p
104 } else {
105 map_lgl(.x, .p, ...)
106 }
107 }
108
109 keep <- function(.x, .f, ...) {
110 .x[probe(.x, .f, ...)]
111 }
112 discard <- function(.x, .p, ...) {
113 sel <- probe(.x, .p, ...)
114 .x[is.na(sel) | !sel]
115 }
116 map_if <- function(.x, .p, .f, ...) {
117 matches <- probe(.x, .p)
118 .x[matches] <- map(.x[matches], .f, ...)
119 .x
120 }
121
122 compact <- function(.x) {
123 Filter(length, .x)
124 }
125
126 transpose <- function(.l) {
127 inner_names <- names(.l[[1]])
128 if (is.null(inner_names)) {
129 fields <- seq_along(.l[[1]])
130 } else {
131 fields <- set_names(inner_names)
132 }
133
134 map(fields, function(i) {
135 map(.l, .subset2, i)
136 })
137 }
138
139 every <- function(.x, .p, ...) {
140 for (i in seq_along(.x)) {
141 if (!rlang::is_true(.p(.x[[i]], ...))) {
142 return(FALSE)
143 }
144 }
145 TRUE
146 }
147 some <- function(.x, .p, ...) {
148 for (i in seq_along(.x)) {
149 if (rlang::is_true(.p(.x[[i]], ...))) {
150 return(TRUE)
151 }
152 }
153 FALSE
154 }
155 negate <- function(.p) {
156 function(...) !.p(...)
157 }
158
159 reduce <- function(.x, .f, ..., .init) {
160 f <- function(x, y) .f(x, y, ...)
161 Reduce(f, .x, init = .init)
162 }
163 reduce_right <- function(.x, .f, ..., .init) {
164 f <- function(x, y) .f(y, x, ...)
165 Reduce(f, .x, init = .init, right = TRUE)
166 }
167 accumulate <- function(.x, .f, ..., .init) {
168 f <- function(x, y) .f(x, y, ...)
169 Reduce(f, .x, init = .init, accumulate = TRUE)
170 }
171 accumulate_right <- function(.x, .f, ..., .init) {
172 f <- function(x, y) .f(y, x, ...)
173 Reduce(f, .x, init = .init, right = TRUE, accumulate = TRUE)
174 }
175
176 detect <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
177 for (i in index(.x, .right)) {
178 if (.p(.f(.x[[i]], ...))) {
179 return(.x[[i]])
180 }
181 }
182 NULL
183 }
184 detect_index <- function(.x, .f, ..., .right = FALSE, .p = is_true) {
185 for (i in index(.x, .right)) {
186 if (.p(.f(.x[[i]], ...))) {
187 return(i)
188 }
189 }
190 0L
191 }
192 index <- function(x, right = FALSE) {
193 idx <- seq_along(x)
194 if (right) {
195 idx <- rev(idx)
196 }
197 idx
198 }
199
200 imap <- function(.x, .f, ...) {
201 map2(.x, vec_index(.x), .f, ...)
202 }
203 vec_index <- function(x) {
204 names(x) %||% seq_along(x)
205 }
206
207
208 # nocov end