From 4e06ab26069d3366963d1a92773de34d53bb49e1 Mon Sep 17 00:00:00 2001 From: Jerry Kang Date: Wed, 8 May 2019 16:03:48 +0800 Subject: [PATCH 1/3] replace other memoised functions in args with original bodies before hashing --- R/memoise.R | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/R/memoise.R b/R/memoise.R index 48915d5..f2bc63e 100644 --- a/R/memoise.R +++ b/R/memoise.R @@ -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)))) @@ -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) @@ -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 @@ -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 { From f12d1617006ea29f858a74a1b090398b80daf748 Mon Sep 17 00:00:00 2001 From: Jerry Kang Date: Wed, 8 May 2019 18:41:03 +0800 Subject: [PATCH 2/3] add test for memoised function as argument --- tests/testthat/test-memoise.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testthat/test-memoise.R b/tests/testthat/test-memoise.R index 89ccac1..eb32d36 100644 --- a/tests/testthat/test-memoise.R +++ b/tests/testthat/test-memoise.R @@ -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) From 846e28325ead2dee3e9dd374425dd80782e35217 Mon Sep 17 00:00:00 2001 From: Jerry Kang Date: Thu, 9 May 2019 21:54:03 +0800 Subject: [PATCH 3/3] Revert "I don't think it needs googleAuthR explicitly", fix #87 This reverts commit 7f0325f10ad1688d40522d99dd690b7279f32b9e. --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 94b3e04..d652cff 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ Suggests: aws.s3, httr, covr, + googleAuthR, googleCloudStorageR License: MIT + file LICENSE RoxygenNote: 6.1.0