R/freduce.R

Defines functions freduce

Documented in freduce

#' Apply a list of functions sequentially
#'
#' This function applies the first function to `value`, then the
#' next function to the result of the previous function call, etc. 
#' 
#' @param value initial value.
#' @param function_list a list of functions.
#' @return The result after applying each function in turn.
#'
#'
#' @export
freduce <- function(value, function_list) {
  k <- length(function_list)
  if (k > 1) {
    for (i in 1:(k - 1L)) {
      value <- function_list[[i]](value)
    }
  }

  value <- withVisible(function_list[[k]](value))

  if (value[["visible"]]) {
    value[["value"]]
  } else {
    invisible(value[["value"]])
  }
}

Try the magrittr package in your browser

Any scripts or data that you put into this service are public.

magrittr documentation built on March 30, 2022, 9:07 a.m.