Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Last active April 11, 2018 19:36
Show Gist options
  • Save Torvaney/ed5bc0cc7c9529027c150c09e95947c6 to your computer and use it in GitHub Desktop.
Save Torvaney/ed5bc0cc7c9529027c150c09e95947c6 to your computer and use it in GitHub Desktop.
Experiments with dots, quoting and purrr
log <- function(f, ...) {
function_name <- as.character(substitute(f))
dots <- list(...)
args <- paste0(dots, collapse = ",")
function_result <- f(...)
print(stringr::str_glue("{function_name}({args}) -> {function_result}"))
function_result
}
logify <- function(f) {
function_name <- as.character(substitute(f))
f_with_log <- function(...) {
dots <- list(...)
args <- paste0(dots, collapse = ",")
function_result <- f(...)
print(stringr::str_glue("{function_name}({args}) -> {function_result}"))
function_result
}
}
# Examples
my_value <- purrr::reduce(c(T, F, F, T), logify(`==`))
add2 <- function(x) x + 2
logify(add2)(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment