memoise icon indicating copy to clipboard operation
memoise copied to clipboard

function to be memoised with argument that is function, which might be passed another memoised function

Open bluaze opened this issue 5 years ago • 1 comments

a function to be memoised, say f, has an argument fn that accepts function. If such argument fn is passed another memoised function, say gm <- memoise(g), the memoised f's cache may not hit as the hash in the memoised f would change when gm is executed with new arguments and new key-value pairs are stored to gm's cache and the hash digest not only the gm itself but also its enclosing environment, which contains the caching environment

here is an example

f <- function(x) x
g <- function(fn) {i <<- fn(i) + 1; i}
i <- 0

fm <- memoise(f)
gm <- memoise(g)

gm(fm)  # first time, would be 1
gm(fm)  # second time, depends on how fm is treated in hashing, preferably still be 1

to avoid hash the entire gm, the original g could be used instead, and to avoid g's own enclosing environment problem and srcref problem, as can be seen in #84, as.character(body(g)) might be more appropriate

bluaze avatar May 08 '19 11:05 bluaze

Codecov Report

Merging #86 into master will decrease coverage by 17.46%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #86       +/-   ##
==========================================
- Coverage   77.37%   59.9%   -17.47%     
==========================================
  Files           5       5               
  Lines         221     222        +1     
==========================================
- Hits          171     133       -38     
- Misses         50      89       +39
Impacted Files Coverage Δ
R/memoise.R 100% <100%> (ø) :arrow_up:
R/cache_s3.R 0% <0%> (-100%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1650ad7...5cac987. Read the comment docs.

codecov-io avatar May 09 '19 14:05 codecov-io