From 3cf32348d1f4a0837fbb4591740f628a178d07d2 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Mon, 6 Mar 2023 10:54:55 +0000 Subject: [PATCH 1/2] Apply `omit_args` also to default arguments --- NEWS.md | 2 ++ R/memoise.R | 6 +++--- tests/testthat/test-memoise.R | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7c2501d..8b8a0a2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # memoise (development version) +* `omit_args` now also works for default arguments (@mgirlich, #145). + # memoise 2.0.1 # Version 2.0.0.9000 diff --git a/R/memoise.R b/R/memoise.R index 673be92..96deedc 100644 --- a/R/memoise.R +++ b/R/memoise.R @@ -148,13 +148,13 @@ memoise <- memoize <- function( # That has not been called default_args <- default_args[setdiff(names(default_args), names(called_args))] - # Ignored specified arguments when hashing - called_args[encl$`_omit_args`] <- NULL - # Evaluate all the arguments args <- c(lapply(called_args, eval, parent.frame()), lapply(default_args, eval, envir = environment())) + # Ignored specified arguments when hashing + args[encl$`_omit_args`] <- NULL + key <- encl$`_hash`( c( encl$`_f_hash`, diff --git a/tests/testthat/test-memoise.R b/tests/testthat/test-memoise.R index bb26a4a..4ff7dda 100644 --- a/tests/testthat/test-memoise.R +++ b/tests/testthat/test-memoise.R @@ -326,6 +326,20 @@ test_that("omit_args respected", { res2 <- mem_rnorm(10, mean = +100) expect_true(identical(res1, res2)) + + # Also works for default args + f <- function(n, x = rnorm(1)) { + rnorm(n) + } + mem_f <- memoise(f) + expect_false(identical(res1, res2)) + res1 <- mem_f(1) + res2 <- mem_f(1) + + mem_f2 <- memoise(f, omit_args = "x") + res1 <- mem_f2(1) + res2 <- mem_f2(1) + expect_true(identical(res1, res2)) }) context("has_cache") From de31e741383756e02d566275627a758780e73892 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Fri, 10 Mar 2023 07:08:25 +0000 Subject: [PATCH 2/2] Make test more clear --- tests/testthat/test-memoise.R | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-memoise.R b/tests/testthat/test-memoise.R index 4ff7dda..b36032b 100644 --- a/tests/testthat/test-memoise.R +++ b/tests/testthat/test-memoise.R @@ -327,19 +327,28 @@ test_that("omit_args respected", { expect_true(identical(res1, res2)) - # Also works for default args - f <- function(n, x = rnorm(1)) { - rnorm(n) + # Also works for default arguments + a <- 0 + f <- function(x = a) { + a <<- a + 1 + a } - mem_f <- memoise(f) - expect_false(identical(res1, res2)) - res1 <- mem_f(1) - res2 <- mem_f(1) + # everytime `f()` is called its value increases by 1 + expect_equal(f(), 1) + expect_equal(f(), 2) + + # it still increases by one when memoised as the argument `x` changes + a <- 0 + mem_f <- memoise::memoise(f) + expect_equal(mem_f(), 1) + expect_equal(mem_f(), 2) + + # but `x` can be ignored via `omit_args` + a <- 0 mem_f2 <- memoise(f, omit_args = "x") - res1 <- mem_f2(1) - res2 <- mem_f2(1) - expect_true(identical(res1, res2)) + expect_equal(mem_f2(), 1) + expect_equal(mem_f2(), 1) }) context("has_cache")