Skip to content

Commit 588007a

Browse files
authored
Merge pull request #146 from mgirlich/omit-args-for-default-args
Apply `omit_args` also to default arguments
2 parents 3b0bf3e + de31e74 commit 588007a

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# memoise (development version)
22

3+
* `omit_args` now also works for default arguments (@mgirlich, #145).
4+
35
# memoise 2.0.1
46

57
# Version 2.0.0.9000

R/memoise.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ memoise <- memoize <- function(
148148
# That has not been called
149149
default_args <- default_args[setdiff(names(default_args), names(called_args))]
150150

151-
# Ignored specified arguments when hashing
152-
called_args[encl$`_omit_args`] <- NULL
153-
154151
# Evaluate all the arguments
155152
args <- c(lapply(called_args, eval, parent.frame()),
156153
lapply(default_args, eval, envir = environment()))
157154

155+
# Ignored specified arguments when hashing
156+
args[encl$`_omit_args`] <- NULL
157+
158158
key <- encl$`_hash`(
159159
c(
160160
encl$`_f_hash`,

tests/testthat/test-memoise.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,29 @@ test_that("omit_args respected", {
326326
res2 <- mem_rnorm(10, mean = +100)
327327

328328
expect_true(identical(res1, res2))
329+
330+
# Also works for default arguments
331+
a <- 0
332+
f <- function(x = a) {
333+
a <<- a + 1
334+
a
335+
}
336+
337+
# everytime `f()` is called its value increases by 1
338+
expect_equal(f(), 1)
339+
expect_equal(f(), 2)
340+
341+
# it still increases by one when memoised as the argument `x` changes
342+
a <- 0
343+
mem_f <- memoise::memoise(f)
344+
expect_equal(mem_f(), 1)
345+
expect_equal(mem_f(), 2)
346+
347+
# but `x` can be ignored via `omit_args`
348+
a <- 0
349+
mem_f2 <- memoise(f, omit_args = "x")
350+
expect_equal(mem_f2(), 1)
351+
expect_equal(mem_f2(), 1)
329352
})
330353

331354
context("has_cache")

0 commit comments

Comments
 (0)