8000 New function has.cache by dkesh · Pull Request #10 · r-lib/memoise · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

New function has.cache #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2016
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2 (4.0.0): do not edit by hand

export(forget)
export(has.cache)
export(is.memoised)
export(is.memoized)
export(memoise)
Expand Down
21 changes: 21 additions & 0 deletions R/memoise.r
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,24 @@ forget <- function(f) {
is.memoised <- is.memoized <- function(f) {
identical(attr(f, "memoised"), TRUE)
}

#' Test whether a memoised function has been cached for particular arguments.
#' @param f Function to test.
#' @param ... arguments to function.
#' @seealso \code{\link{is.memoised}}, \code{\link{memoise}}
#' @export has.cache
#' @examples
#' mem_sum <- memoise(sum)
#' has.cache( mem_sum, 1, 2, 3) # FALSE
#' mem_sum(1, 2, 3)
#' has.cache( mem_sum, 1, 2, 3) # TRUE
has.cache <- function(f, ...) {
if(!is.memoised(f)) return(FALSE)

hash <- digest(list(...))
env <- environment(f)
if (!exists("cache", env, inherits = FALSE)) return(FALSE)
cache <- get("cache", env)

cache$has_key(hash)
}
26 changes: 26 additions & 0 deletions man/has.cache.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/memoise.r
\name{has.cache}
\alias{has.cache}
\title{Test whether a memoised function has been cached for particular arguments.}
\usage{
has.cache(f, ...)
}
\arguments{
\item{f}{Function to test.}

\item{...}{arguments to function.}
}
\description{
Test whether a memoised function has been cached for particular arguments.
}
\examples{
mem_sum <- memoise(sum)
has.cache( mem_sum, 1, 2, 3) # FALSE
mem_sum(1, 2, 3)
has.cache( mem_sum, 1, 2, 3) # TRUE
}
\seealso{
\code{\link{is.memoised}}, \code{\link{memoise}}
}

0