Title: | Deferred List - A Read-Only List-Like Object with Deferred Access |
---|---|
Description: | Implements the 'deflist' class, a read-only list-like object that accesses its elements via a function. The 'deflist' class can be used to model deferred access to data or computations by routing indexed list access to a function. This approach is particularly useful when sequential list-like access to data is required but holding all the data in memory at once is not feasible. The package also provides utilities for memoisation and caching to optimize access to frequently requested elements. |
Authors: | Bradley Buchsbaum [aut, cre] |
Maintainer: | Bradley Buchsbaum <[email protected]> |
License: | LGPL (>= 2.1) |
Version: | 0.2.0 |
Built: | 2024-11-14 06:00:30 UTC |
Source: | https://github.com/bbuchsbaum/deflist |
Subset a deflist object
## S3 method for class 'deflist' x[i]
## S3 method for class 'deflist' x[i]
x |
A deflist object. |
i |
Indices or names of the elements to be retrieved. |
A list containing the elements at the specified indices or names in the deflist
object.
Retrieve an element from a deflist object
## S3 method for class 'deflist' x[[i]]
## S3 method for class 'deflist' x[[i]]
x |
A deflist object. |
i |
Index or name of the element to be retrieved. |
The element at the specified index or name in the deflist object.
Prevent assignment to an element in a deflist object
## S3 replacement method for class 'deflist' x[[i]] <- value
## S3 replacement method for class 'deflist' x[[i]] <- value
x |
A deflist object. |
i |
Index or name of the element to be assigned. |
value |
Value to be assigned to the element. |
this function throws an error be design, no return value
Prevent assignment to elements in a deflist object
## S3 replacement method for class 'deflist' x[i] <- value
## S3 replacement method for class 'deflist' x[i] <- value
x |
A deflist object. |
i |
Indices or names of the elements to be assigned. |
value |
Values to be assigned to the elements. |
this function throws an error be design, no return value
Convert a deflist object to a list
## S3 method for class 'deflist' as.list(x, ...)
## S3 method for class 'deflist' as.list(x, ...)
x |
A deflist object. |
... |
Additional arguments passed to methods. |
A list containing the elements of the deflist object.
A read-only list that retrieves elements with a function call. The deferred list is useful for handling large datasets where elements are computed on-demand.
deflist( fun, len = 1, names, memoise = FALSE, cache = c("memory", "file"), cachedir = NULL )
deflist( fun, len = 1, names, memoise = FALSE, cache = c("memory", "file"), cachedir = NULL )
fun |
A function that is used to retrieve elements. |
len |
Integer, the length of the list (default is 1). |
names |
Character vector, an optional set of names, one per element. |
memoise |
Logical, whether to memoise the function to speed up repeated element access (default is FALSE). |
cache |
Character, use an in-memory or filesystem cache if |
cachedir |
Character, the file path to the cache (default is NULL). |
The deferred list is created using the provided function, length, names, and caching options. The list is read-only, and elements are retrieved using the provided function.
An object of class "deflist" representing the deferred list.
# Create a deferred list of squares square_fun <- function(i) i^2 square_deflist <- deflist(square_fun, len = 5) print(square_deflist) cat("First element of the list:", square_deflist[[1]], "\n")
# Create a deferred list of squares square_fun <- function(i) i^2 square_deflist <- deflist(square_fun, len = 5) print(square_deflist) cat("First element of the list:", square_deflist[[1]], "\n")
Retrieve the length of a deflist object
## S3 method for class 'deflist' length(x)
## S3 method for class 'deflist' length(x)
x |
A deflist object. |
The length of the deflist object.
square_fun <- function(i) i^2 square_deflist <- deflist(square_fun, len = 5) stopifnot(length(square_deflist) == 5)
square_fun <- function(i) i^2 square_deflist <- deflist(square_fun, len = 5) stopifnot(length(square_deflist) == 5)