Skip to content

Proposal for an alternative to achieve caching #1651

@reitzig

Description

@reitzig

I realize that API styles are a matter of taste. FWIW, I think this is a nicer way of achieving caching functionality since it's harde to misuse:

makeVector <- function(x = numeric()) {
  m <- NULL

  set <- function(y) {
    x <<- y
    m <<- NULL
  }

  get <- function() x

  mean <- function() {
    if ( is.null(m) ) {
      # Note that mean(x) would not work -- then the function would
      # call itself!
      m <<- base::mean(x)
    }
    m
  }

  list(set = set, get = get, mean = mean)
}

versioned here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions