memoise/0000755000175100001440000000000011440240527011725 5ustar hornikusersmemoise/DESCRIPTION0000644000175100001440000000071111440247266013441 0ustar hornikusersPackage: memoise Title: Memoise functions Version: 0.1 Author: Hadley Wickham Maintainer: Hadley Wickham Description: Cache the results of a function so that when you call it again with the same arguments it returns the pre-computed value. Imports: digest License: MIT Collate: 'cache.r' 'memoise.r' Packaged: 2010-09-03 18:41:27 UTC; hadley Repository: CRAN Date/Publication: 2010-09-03 19:38:30 memoise/man/0000755000175100001440000000000011440240077012500 5ustar hornikusersmemoise/man/forget.Rd0000644000175100001440000000031111440240077014250 0ustar hornikusers\name{forget} \alias{forget} \title{Forget past results.} \usage{forget(f)} \description{ Forget past results. Resets the cache of a memoised function. } \arguments{ \item{f}{memoised function} } memoise/man/memoize.Rd0000644000175100001440000000071711440240240014431 0ustar hornikusers\name{memoize} \title{Memoise a function.} \usage{memoize(f)} \description{ Memoise a function. } \seealso{\url{http://en.wikipedia.org/wiki/Memoization}} \alias{memoise} \alias{memoize} \arguments{ \item{f}{function to memoise} } \examples{a <- function(x) runif(1) replicate(10, a()) b <- memoise(a) replicate(10, b()) c <- memoise(function(x) { Sys.sleep(1); runif(1) }) system.time(print(c())) system.time(print(c())) forget(c) system.time(print(c()))} memoise/NAMESPACE0000644000175100001440000000010311440240240013126 0ustar hornikusersexport(memoise, memoize) importFrom(digest, digest) export(forget) memoise/R/0000755000175100001440000000000011440236351012126 5ustar hornikusersmemoise/R/cache.r0000644000175100001440000000102511440236364013356 0ustar hornikusersnew_cache <- function() { cache <- NULL cache_reset <- function() { cache <<- new.env(TRUE, emptyenv()) } cache_set <- function(key, value) { assign(key, value, env = cache) } cache_get <- function(key) { get(key, env = cache, inherits = FALSE) } cache_has_key <- function(key) { exists(key, env = cache, inherits = FALSE) } cache_reset() list( reset = cache_reset, set = cache_set, get = cache_get, has_key = cache_has_key, keys = function() ls(cache) ) }memoise/R/memoise.r0000644000175100001440000000200611440240215013737 0ustar hornikusers#' Memoise a function. #' #' @param f function to memoise #' @seealso \url{http://en.wikipedia.org/wiki/Memoization} #' @aliases memoise memoize #' @export memoise memoize #' @importFrom digest digest #' @examples #' a <- function(x) runif(1) #' replicate(10, a()) #' b <- memoise(a) #' replicate(10, b()) #' #' c <- memoise(function(x) { Sys.sleep(1); runif(1) }) #' system.time(print(c())) #' system.time(print(c())) #' forget(c) #' system.time(print(c())) memoize <- memoise <- function(f) { cache <- new_cache() function(...) { hash <- digest(list(...)) if (cache$has_key(hash)) { cache$get(hash) } else { res <- f(...) cache$set(hash, res) res } } } #' Forget past results. #' Resets the cache of a memoised function. #' #' @param f memoised function #' @export forget <- function(f) { if (!is.function(f)) return(FALSE) env <- environment(f) if (!exists("cache", env, inherits = FALSE)) return(FALSE) cache <- get("cache", env) cache$reset() TRUE }