Package 'deflist'

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

Help Index


Subset a deflist object

Description

Subset a deflist object

Usage

## S3 method for class 'deflist'
x[i]

Arguments

x

A deflist object.

i

Indices or names of the elements to be retrieved.

Value

A list containing the elements at the specified indices or names in the deflist object.


Retrieve an element from a deflist object

Description

Retrieve an element from a deflist object

Usage

## S3 method for class 'deflist'
x[[i]]

Arguments

x

A deflist object.

i

Index or name of the element to be retrieved.

Value

The element at the specified index or name in the deflist object.


Prevent assignment to an element in a deflist object

Description

Prevent assignment to an element in a deflist object

Usage

## S3 replacement method for class 'deflist'
x[[i]] <- value

Arguments

x

A deflist object.

i

Index or name of the element to be assigned.

value

Value to be assigned to the element.

Value

this function throws an error be design, no return value


Prevent assignment to elements in a deflist object

Description

Prevent assignment to elements in a deflist object

Usage

## S3 replacement method for class 'deflist'
x[i] <- value

Arguments

x

A deflist object.

i

Indices or names of the elements to be assigned.

value

Values to be assigned to the elements.

Value

this function throws an error be design, no return value


Convert a deflist object to a list

Description

Convert a deflist object to a list

Usage

## S3 method for class 'deflist'
as.list(x, ...)

Arguments

x

A deflist object.

...

Additional arguments passed to methods.

Value

A list containing the elements of the deflist object.


Create a deferred list

Description

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.

Usage

deflist(
  fun,
  len = 1,
  names,
  memoise = FALSE,
  cache = c("memory", "file"),
  cachedir = NULL
)

Arguments

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 memoise is TRUE (default is "memory").

cachedir

Character, the file path to the cache (default is NULL).

Details

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.

Value

An object of class "deflist" representing the deferred list.

Examples

# 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

Description

Retrieve the length of a deflist object

Usage

## S3 method for class 'deflist'
length(x)

Arguments

x

A deflist object.

Value

The length of the deflist object.

Examples

square_fun <- function(i) i^2
square_deflist <- deflist(square_fun, len = 5)
stopifnot(length(square_deflist) == 5)