R/addMemoization.R

Defines functions addMemoization

Documented in addMemoization

#' Add Memoization to a Function
#'
#' @param fcn the function to be memoized
#' @param verbose show debugging information
#' 
#' @examples 
#' runifCached <- addMemoization(runif)
#'
#' @return memoized function
#' @export
addMemoization <- function(fcn, verbose=FALSE) {
  if(verbose) {
    cat("In modified addMemoization()", sep = "\n")
  }
  
  if (!is.function(fcn)) {
    stop("Argument 'fcn' is not a function");
  }

  # Already memoized?
  if (inherits(fcn, "MemoizedFunction")) {
    return(fcn)
  }

  res <- function(...) {
    args <- list(fcn, ...)
    do.call("memoizedCall", args=args)
  }
  class(res) <- c("MemoizedFunction", class(res))

  res
} 

Try the simpleRCache package in your browser

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

simpleRCache documentation built on July 16, 2021, 1:07 a.m.