Hauke L.
Hauke L.

Reputation: 293

Is it ok to memoise a nested function?

I have a function of the below form. I vectorized it using purrr::map rather than using vectorize following this advise.

load_data <- function(key){
  load_data_one <- function(key){
    ...
    # somehow retrieve data based on key
    ...
    data
  }
  # create result allowing key to be a vector
  result <- map_dfr(key, load_data_one) 
}

The routine to retrieve data is quite expensive. Having discovered the memoise package, I want to figure out the best way to use it in this situation. Ideally, I want to put it around the nested function load_data_one, so I imagine that when I call load_data for a vector, than only the unknown results would be calculated. If I memoise load_data instead, and I pass one vector only slightly different to another one that I passed earlier, would the memoise function be smart enough to only calculate the new elements? Also, are there any other issues related to applying memoise to the nested function load_data_one, that would require me to maybe put it outside rather than within load_data?

Upvotes: 0

Views: 71

Answers (1)

r2evans
r2evans

Reputation: 160447

I suggest you define and memoise the load_data_one function externally:

load_data_one <- memoise(function(key){
  ...
  # somehow retrieve data based on key
  ...
  data
})

load_data <- function(key){
  # create result allowing key to be a vector
  result <- map_dfr(key, load_data_one) 
}

This way, the memoization is done per individual key, so if you start with load_data(c(1,3)) and then call load_data(c(1,2,3)), the second call will only calculate for 2, recalling the memoized results for 1 and 3.

Upvotes: 2

Related Questions