yulab-cache: cache intermediate data

initial_cacheR Documentation

cache intermediate data

Description

Yulab provides a set of utilities to cache intermediate data, including initialize the cached item, update cached item and rmove the cached item, etc.

Usage

initial_cache()

get_cache()

rm_cache()

initial_cache_item(item)

get_cache_item(item)

rm_cache_item(item)

update_cache_item(item, elements)

get_cache_element(item, elements)

Arguments

item

the name of the cached item

elements

elements to be cached in the item

Value

return the cache environment, item or selected elements, depends on the functions.

Examples

## Not run: 
 slow_fib <- function(x) {
     if (x < 2) return(1)
     slow_fib(x-2) + slow_fib(x-1)
 }
 
 fast_fib <- function(x) {
     if (x < 2) return(1)
     res <- get_cache_element('fibonacci', as.character(x))
     if (!is.null(res)) { 
         return(res)
     }
     res <- fast_fib(x-2) + fast_fib(x-1)
     e <- list()
     e[[as.character(x)]] <- res
     update_cache_item('fibonacci', e)
     return(res)
 }

 system.time(slow_fib(30))
 system.time(fast_fib(30)) 
    
 
## End(Not run)

yulab.utils documentation built on Sept. 11, 2024, 9:25 p.m.