Skip to content

Commit

Permalink
Merge pull request #146 from mgirlich/omit-args-for-default-args
Browse files Browse the repository at this point in the history
Apply `omit_args` also to default arguments
  • Loading branch information
wch committed Mar 10, 2023
2 parents 3b0bf3e + de31e74 commit 588007a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions R/memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,29 @@ test_that("omit_args respected", {
res2 <- mem_rnorm(10, mean = +100)

expect_true(identical(res1, res2))

# Also works for default arguments
a <- 0
f <- function(x = a) {
a <<- a + 1
a
}

# 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")
expect_equal(mem_f2(), 1)
expect_equal(mem_f2(), 1)
})

context("has_cache")
Expand Down

0 comments on commit 588007a

Please sign in to comment.