Debian Codegraph @

R/utils-map.R

raw
1
2 bapply <- function(x, f, ..., index = "Index") {
3 result <- lapply(x, f, ...)
4 bind(result, index = index)
5 }
6
7 enumerate <- function(x, f, ..., FUN.VALUE = NULL) {
8
9 n <- names(x)
10 idx <- `names<-`(seq_along(x), n)
11 callback <- function(i) f(n[[i]], x[[i]], ...)
12
13 if (is.environment(x))
14 x <- as.list(x, all.names = TRUE)
15
16 if (is.null(FUN.VALUE))
17 lapply(idx, callback)
18 else
19 vapply(idx, callback, FUN.VALUE = FUN.VALUE)
20
21 }
22
23 enum_chr <- function(x, f, ...) {
24 enumerate(x, f, ..., FUN.VALUE = character(1))
25 }
26
27 enum_int <- function(x, f, ...) {
28 enumerate(x, f, ..., FUN.VALUE = integer(1))
29 }
30
31 enum_dbl <- function(x, f, ...) {
32 enumerate(x, f, ..., FUN.VALUE = double(1))
33 }
34
35 enum_lgl <- function(x, f, ...) {
36 enumerate(x, f, ..., FUN.VALUE = logical(1))
37 }
38
39
40 uapply <- function(x, f, ...) {
41 f <- match.fun(f)
42 unlist(lapply(x, f, ...), recursive = FALSE)
43 }
44
45 filter <- function(x, f, ...) {
46 f <- match.fun(f)
47 x[map_lgl(x, f, ...)]
48 }
49
50 reject <- function(x, f, ...) {
51 f <- match.fun(f)
52 x[!map_lgl(x, f, ...)]
53 }
54
55 map <- function(x, f, ...) {
56 f <- match.fun(f)
57 lapply(x, f, ...)
58 }
59
60 map_chr <- function(x, f, ...) {
61 f <- match.fun(f)
62 vapply(x, f, ..., FUN.VALUE = character(1))
63 }
64
65 map_dbl <- function(x, f, ...) {
66 f <- match.fun(f)
67 vapply(x, f, ..., FUN.VALUE = numeric(1))
68 }
69
70 map_int <- function(x, f, ...) {
71 f <- match.fun(f)
72 vapply(x, f, ..., FUN.VALUE = integer(1))
73 }
74
75 map_lgl <- function(x, f, ...) {
76 f <- match.fun(f)
77 vapply(x, f, ..., FUN.VALUE = logical(1))
78 }
79
80
81 extract <- function(x, ...) {
82 lapply(x, `[[`, ...)
83 }
84
85 extract_chr <- function(x, ...) {
86 vapply(x, `[[`, ..., FUN.VALUE = character(1))
87 }
88
89 extract_dbl <- function(x, ...) {
90 vapply(x, `[[`, ..., FUN.VALUE = numeric(1))
91 }
92
93 extract_int <- function(x, ...) {
94 vapply(x, `[[`, ..., FUN.VALUE = integer(1))
95 }
96
97 extract_lgl <- function(x, ...) {
98 vapply(x, `[[`, ..., FUN.VALUE = logical(1))
99 }