Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Suggests:
aws.s3,
httr,
covr,
googleAuthR,
googleCloudStorageR
License: MIT + file LICENSE
RoxygenNote: 6.1.0
9 changes: 6 additions & 3 deletions R/memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ memoise <- memoize <- function(f, ..., envir = environment(f), cache = cache_mem
args <- c(lapply(called_args, eval, parent.frame()),
lapply(default_args, eval, envir = environment()))

# Replace memoised functions in arguments with their original bodies
args <- lapply(args, function(x) if (memoise::is.memoised(x)) as.character(body(environment(x)$`_f`)) else x)

hash <- encl$`_cache`$digest(
c(as.character(body(encl$`_f`)), args,
lapply(encl$`_additional`, function(x) eval(x[[2L]], environment(x))))
Expand All @@ -147,7 +150,7 @@ memoise <- memoize <- function(f, ..., envir = environment(f), cache = cache_mem

# This should only happen for primitive functions
if (is.null(envir)) {
envir <- baseenv()
envir <- baseenv()
}

memo_f_env <- new.env(parent = envir)
Expand Down Expand Up @@ -261,7 +264,7 @@ has_cache <- function(f) {
# Modify the function body of the function to simply return TRUE and FALSE
# rather than get or set the results of the cache
body <- body(f)
body[[9]] <- quote(if (encl$`_cache`$has_key(hash)) return(TRUE) else return(FALSE))
body[[10]] <- quote(if (encl$`_cache`$has_key(hash)) return(TRUE) else return(FALSE))
body(f) <- body

f
Expand All @@ -288,7 +291,7 @@ drop_cache <- function(f) {
# Modify the function body of the function to simply drop the key
# and return TRUE if successfully removed
body <- body(f)
body[[9]] <- quote(if (encl$`_cache`$has_key(hash)) {
body[[10]] <- quote(if (encl$`_cache`$has_key(hash)) {
encl$`_cache`$drop_key(hash)
return(TRUE)
} else {
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ test_that("argument names don't clash with names in memoised function body", {
expect_identical(f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), f_mem(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
})

test_that("other memoised function passed as arguments", {
f <- function(x) x
g <- function(fn) {i <<- fn(i) + 1; i}
i <- 0

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

expect_equal(g(fm), 1)
expect_equal(gm(fm), 2)
expect_equal(gm(fm), 2)
expect_equal(g(fm), 3)
expect_equal(gm(fm), 2)
})

context("has_cache")
test_that("it works as expected with memoised functions", {
mem_sum <- memoise(sum)
Expand Down