mirror of https://git.sr.ht/~hrbrmstr/freebase
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
997 B
39 lines
997 B
# NOTE these aren't 100% equivalent to the purrr walkers but cover very common use-cases
|
|
#
|
|
# NOTE formula function (e.g. ~{}) are 100% supported
|
|
|
|
# walk(LETTERS, ~cat(tolower(.x)))
|
|
walk <- function(.x, .f, ...) {
|
|
|
|
if (inherits(.f, "formula")) {
|
|
.body <- dimnames(attr(terms(.f), "factors"))[[1]]
|
|
.f <- function(.x, . = .x) {}
|
|
body(.f) <- as.expression(parse(text=.body))
|
|
}
|
|
|
|
if (inherits(.f, "function")) {
|
|
for (idx in seq_along(.x)) .f(.x[[idx]], ...)
|
|
} else {
|
|
stop("I'm not sure indexing by name or number makes sense for walk().")
|
|
}
|
|
|
|
invisible(.x)
|
|
|
|
}
|
|
|
|
# walk2(LETTERS, letters, ~cat(tolower(.x), rep(toupper(.y), 4), sep=""))
|
|
walk2 <- function(.x, .y, .f, ...) {
|
|
|
|
if (inherits(.f, "formula")) {
|
|
.body <- dimnames(attr(terms(.f), "factors"))[[1]]
|
|
.f <- function(.x, .y, . = .x) {}
|
|
body(.f) <- as.expression(parse(text=.body))
|
|
}
|
|
|
|
if (inherits(.f, "function")) {
|
|
for (idx in seq_along(.x)) .f(.x[[idx]], .y[[idx]], ...)
|
|
}
|
|
|
|
invisible(.x)
|
|
|
|
}
|
|
|