Skip to content
Merged
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
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ URL: https://github.com/hadley/memoise
BugReports: https://github.com/hadley/memoise/issues
Imports:
digest
Suggests: testthat
Suggests:
testthat
License: MIT + file LICENSE
RoxygenNote: 5.0.0
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2 (4.1.1): do not edit by hand
# Generated by roxygen2: do not edit by hand

S3method(print,memoised)
export(forget)
export(is.memoised)
export(is.memoized)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Version 0.2.99.9000

* Memoised functions now have a print method which displays the original
function definition, rather than the memoisation code (#15, @jimhester).
* A memoised function now has the same interface as the original function,
if the original function is known when `memoise` is called. (Otherwise,
the old behavior is invoked, with a warning.) (#14, @krlmlr)
Expand Down
12 changes: 10 additions & 2 deletions R/memoise.r
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ memoise_new <- function(f, envir) {
memo_f_env$digest <- digest
environment(memo_f) <- memo_f_env

class(memo_f) <- c("memoised", "function")

memo_f
}

Expand Down Expand Up @@ -168,10 +170,16 @@ memoise_old <- function(f) {
invisible(res$value)
}
}
attr(memo_f, "memoised") <- TRUE
class(memo_f) <- c("memoised", "function")
memo_f
}

#' @export
print.memoised <- function(x, ...) {
cat("Memoised Function:\n")
print(environment(x)$f)
}

#' Forget past results.
#' Resets the cache of a memoised function.
#'
Expand Down Expand Up @@ -211,5 +219,5 @@ forget <- function(f) {
#' is.memoised(lm) # FALSE
#' is.memoised(mem_lm) # TRUE
is.memoised <- is.memoized <- function(f) {
is.function(f) && identical(attr(f, "memoised"), TRUE)
is.function(f) && inherits(f, "memoised")
}
2 changes: 1 addition & 1 deletion man/forget.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/is.memoised.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions man/memoise.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,19 @@ test_that("can memoise primitive", {
expect_equal(fm(2, 3), 2 + 3)
expect_equal(fm(1, 2), 1 + 2)
})

test_that("printing a memoised function prints the original definition", {

fn <- function(j) { i <<- i + 1; i }

fnm <- memoise(fn)

fn_output <- capture.output(fn)
str(fn_output)
fnm_output <- capture.output(fnm)
str(fnm_output)

expect_equal(fnm_output[1], "Memoised Function:")

expect_equal(fnm_output[-1], fn_output)
})