Title: | Function Memoization |
---|---|
Description: | A simple way to memoize function results to improve performance by eliminating unnecessary computation or data retrieval activities. |
Authors: | Roy Wetherall <[email protected]> |
Maintainer: | Roy Wetherall <[email protected]> |
License: | GPL-3 |
Version: | 1.0.3 |
Built: | 2024-10-25 03:17:14 UTC |
Source: | https://github.com/rwetherall/memofunc |
For a given function and call, return a list of class 'functionCall' which can be hashed to provide a unique identifier for the function and parameters used for this call.
functionCall(f = sys.function(sys.parent()), call = sys.call(sys.parent()))
functionCall(f = sys.function(sys.parent()), call = sys.call(sys.parent()))
f |
function, defaults to the containing function |
call |
call, default to the containing call |
functionCall, a hashable form of the function call information
# my example function my.function <- function (x, y) x+y # create a new function call my.functionCall <- functionCall(my.function, call("my.function", 10, 10)) # the function and arguments are now available my.functionCall$f my.functionCall$args # using the default argument values to get the function call of the containing function my.function2 <- function (x, y) functionCall() my.functionCall2 <- my.function2(10, 10) # the function and arguments are now available my.functionCall2$f my.functionCall2$args
# my example function my.function <- function (x, y) x+y # create a new function call my.functionCall <- functionCall(my.function, call("my.function", 10, 10)) # the function and arguments are now available my.functionCall$f my.functionCall$args # using the default argument values to get the function call of the containing function my.function2 <- function (x, y) functionCall() my.functionCall2 <- my.function2(10, 10) # the function and arguments are now available my.functionCall2$f my.functionCall2$args
Hashes a value into a string.
hash(value)
hash(value)
value |
value to hash |
hashed value as a string
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
Default hash function.
## Default S3 method: hash(value)
## Default S3 method: hash(value)
value |
value to hash |
hashed value as a string
digest::digest(value)
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
Hashes a function, but considering the formals and body, thus the resulting has is influenced by changes to signature and implementation.
## S3 method for class ''function'' hash(value)
## S3 method for class ''function'' hash(value)
value |
value to hash |
hashed value as a string
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
Hashes a function call, taking into account the values provided to the function call and unprovided default values. Ensures the order the parameters are provided does not change the outcome of the hash calculation.
## S3 method for class 'functionCall' hash(value)
## S3 method for class 'functionCall' hash(value)
value |
value to hash |
hashed value as a string
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
Hashes a list of items, generating a single unique hash value which is based on the combination of hashed list items.
## S3 method for class 'list' hash(value)
## S3 method for class 'list' hash(value)
value |
value to hash |
hashed value as a string
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
my.function <- function (x, y) x+y # a list of values to hash values <- list( "Hello world!", 101, 3.142, TRUE, my.function, (function (x, y) x+y), functionCall(my.function, call("my.function", 10, 10)), list(a=1, b=2, c="hello") ) # hash the values in the list (hashes <- lapply(values, hash)) # Note that functions with the same body will have the same hash hashes[[5]] == hashes[[6]]
Checks whether the passed function is a memo function.
is.memo(f)
is.memo(f)
f |
function, memo or otherwise |
TRUE
if memo function, FALSE
otherwise
Creates a memoized function, based on the provided named or anonymous function. Calls to the memoized function will be retrieved from a cache, unless it is the first time it is called.
Passing memo.force = TRUE
to the memo function call will by-pass any previously cached values and execute the underlying
function, storing the newly retrieved values for subsequent calls. memo.force = FALSE
by default.
Passing memo.dryrun = TRUE
to the memo function call will prevent the underlying function from executing and return TRUE
if call isn't caches and FALSE
if it is. These values are not cached as responses for the function.
Note that results are cached based on the argument values passed to the function. The order is not important since all
names are resolved. So fun(a=1, b=2)
will return the same cached value as fun(b=2, a=1)
, for example.
Functions as arguments are supported, but only the body is compared. So a named function parameter and an anonymouse function parameter with the same body, will be evaluated as identical and return the same cached value.
...
is supported, but note that unless named then the order of the values is significant and will produce different cache values
unless identical.
By default NULL
values are not cached. Setting allow.null=TRUE
when creating the memo will, however, ensure that NULL values
are cached.
memo(f, allow.null = FALSE)
memo(f, allow.null = FALSE)
f |
function to memoise |
allow.null |
if |
the memoed function
library(magrittr) # a simple example function simple.function <- function (value) { print("Executing!") value } # call memo function to memoise a function simple.function.memo <- memo(simple.function) # or like this simple.function %<>% memo() # or use an anon function simple.function2 <- (function (value) value) %>% memo() # the first time we call the memo the function will execute simple.function(10) # if we call the memo again with the same parameter values then # the cached value will be returned simple.function(10) # calling the memo with a different set of parameter values will # cause the function to execute simple.function(20) # consider a slow function which is memoised, note that we have used the allow.null argument # so that NULL is cached when returned from a function, the default is FALSE slow.function <- (function (value) Sys.sleep(value)) %>% memo(allow.null = TRUE) # the first time we call the slow function it takes some time system.time(slow.function(3)) # subsequent calls make use of the cache and are much faster system.time(slow.function(3))
library(magrittr) # a simple example function simple.function <- function (value) { print("Executing!") value } # call memo function to memoise a function simple.function.memo <- memo(simple.function) # or like this simple.function %<>% memo() # or use an anon function simple.function2 <- (function (value) value) %>% memo() # the first time we call the memo the function will execute simple.function(10) # if we call the memo again with the same parameter values then # the cached value will be returned simple.function(10) # calling the memo with a different set of parameter values will # cause the function to execute simple.function(20) # consider a slow function which is memoised, note that we have used the allow.null argument # so that NULL is cached when returned from a function, the default is FALSE slow.function <- (function (value) Sys.sleep(value)) %>% memo(allow.null = TRUE) # the first time we call the slow function it takes some time system.time(slow.function(3)) # subsequent calls make use of the cache and are much faster system.time(slow.function(3))
Gets the cache associated with a memo function allowing further manipulation and control of the underlying values being stored.
Execution is stopped if function passed is not a valid memoed function.
memo.cache(f)
memo.cache(f)
f |
memo function |
Cache storing values for memoed function.
Gets the original function that was memoized.
Execution is stopped if function passed is not a valid memoed function.
memo.function(f)
memo.function(f)
f |
memo function |
Original unmemoized function.
The memofunc
package provides a simple way to memoize a function to optimise execution for process or data
intensive actions.
memo
- memoize a function
is.memo
- is the given function a memo
memo.function
- get a memo's origional function
memo.cache
- get a memo's cache storage
Roy Wetherall [email protected]
Clear the given storage of all keys and their values.
storage.clear(storage)
storage.clear(storage)
storage |
initialized storage |
Invisibily returns storage
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Clear the given storage of all keys and their values.
## S3 method for class 'memory' storage.clear(storage)
## S3 method for class 'memory' storage.clear(storage)
storage |
initialized storage |
Invisibily returns storage
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Gets a value, for a given key, from the store.
If there is no coresponding value for the key, then NULL
is returned.
storage.get(storage, key)
storage.get(storage, key)
storage |
initialized storage |
key |
key to retrieve value for |
Stored value for the key, NULL
otherwise.
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Gets a value, for a given key, from the store.
If there is no coresponding value for the key, then NULL
is returned.
## S3 method for class 'memory' storage.get(storage, key)
## S3 method for class 'memory' storage.get(storage, key)
storage |
initialized storage |
key |
key to retrieve value for |
Stored value for the key, NULL
otherwise.
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Indicates if a given key has a associated value stored in the storage or not.
storage.has(storage, key)
storage.has(storage, key)
storage |
initialized storage |
key |
key to check for stored value |
TRUE
if key has an associated stored value, FALSE
otherwise.
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Indicates if a given key has a associated value stored in the storage or not.
## S3 method for class 'memory' storage.has(storage, key)
## S3 method for class 'memory' storage.has(storage, key)
storage |
initialized storage |
key |
key to check for stored value |
TRUE
if key has an associated stored value, FALSE
otherwise.
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Initlaize storage for name value pairs based on provided type.
Available types of storage include:
memory
- transient in-memory storage
file
- persistent storage, using local file storage
Additional paramters may be provided when initializing different types of storage.
See specific storage types for details.
storage.init(storage.type = storage.memory.class, ...)
storage.init(storage.type = storage.memory.class, ...)
storage.type |
storage type to initialize, defaults to |
... |
additional configuration values used by storage implementations |
List containing characteristics perticular to the storage implementation, including:
$type
- the storage type
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Initlaize memory storage, used to hold and retrieve values in memory.
The storage type is expected to specified as memory
.
This storage is transient.
## S3 method for class 'memory' storage.init(storage.type = storage.memory.class, ...)
## S3 method for class 'memory' storage.init(storage.type = storage.memory.class, ...)
storage.type |
storage type to initialize, defaults to |
... |
additional configuration values used by storage implementations |
List containing characteristics perticular to the storage implementation, including:
$type
- the storage type
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Stores a value for a given key.
If there is already a value stored for the key provided, then the exisiting value is overriden with the new value.
storage.set(storage, key, value)
storage.set(storage, key, value)
storage |
initialized storage |
key |
key to store value against |
value |
value to store |
Invisbily returns storage
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Stores a value for a given key.
If there is already a value stored for the key provided, then the exisiting value is overriden with the new value.
## S3 method for class 'memory' storage.set(storage, key, value)
## S3 method for class 'memory' storage.set(storage, key, value)
storage |
initialized storage |
key |
key to store value against |
value |
value to store |
Invisbily returns storage
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Unsets the value stored for a given key.
If there is no value for the key provided no action is taken.
storage.unset(storage, key)
storage.unset(storage, key)
storage |
initialized storage |
key |
key whose value is to be unset |
Invisibily returns storage
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
Unsets the value stored for a given key.
If there is no value for the key provided no action is taken.
## S3 method for class 'memory' storage.unset(storage, key)
## S3 method for class 'memory' storage.unset(storage, key)
storage |
initialized storage |
key |
key whose value is to be unset |
Invisibily returns storage
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
library(magrittr) # initialize default memory storage my.storage <- storage.init() # set a value into storage storage.set(my.storage, "name", "Roy Wetherall") # .. and some more my.storage %>% storage.set("age", 45) %>% storage.set("alive", TRUE) %>% storage.set("children", c("Peter", "Grace", "Lucy")) # check a key has been set if (storage.has(my.storage, "name")) print("I know your name!") # .. and that a key hasn't been set if (!storage.has(my.storage, "address")) print("I don't know where you live!") # get some values from storage sprintf( "%s is %i years old.", storage.get(my.storage, "name"), storage.get(my.storage, "age")) # remove a value from storage storage.unset(my.storage, "children") # .. and show it's not there anymore if (!storage.has(my.storage, "address")) print("I don't know who your children are!") # clear all values from storage storage.clear(my.storage) # .. and everything is gone if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")