Skip to contents

Create a structure.

Usage

struct(..., .class = NULL, .attr = list())

is.struct(x, .class = NULL)

as.struct(x, .class)

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

is.struct_list(x, .class = NULL)

Arguments

...

a set of named values to use in the structure. or an unnamed list of `struct`s of the same type

.class

the class of the nested list

.attr

attributes to assign to the structure

Value

Usually a length one `struct_list` with a single `struct` item with class `.class` and attributes `.attr`. If a list of `struct`s is provided then it will be longer than 1.

Methods (by generic)

  • as.list(struct): Make `struct` into plain list

Functions

  • is.struct(): Check is a `struct`

  • as.struct(): Cast to a `struct`

  • is.struct_list(): is a list of `structs`?

Examples

x = struct(a=1,b=2,c=1:3,.class="test")
format.test = function(x,...) {sprintf("\"%d/%d [%d-%d]\"", x$a, x$b, min(x$c), max(x$c))}
x # is a struct_list of length 1
#> test(a, b, c)
#> 1, 2, 1, 2, 3
x[[1]] # is a struct of length 1
#> 1, 2, 1, 2, 3

# e = struct(.class="test")
try(struct())
#> Error : Item is not a `struct_list`, a `struct` or a uniform list of `structs`.

x =
  struct(
    struct(a=1,b=2,c=1:3,.class="test"),
    struct(a=2,b=3,c=4:5,.class="test")
  )

tmp = lapply(1:10, function(x) struct(a=x,b=3,c=4:5,.class="test"))
struct(tmp)
#> test(a, b, c)
#> 1, 3, 4, 5 2, 3, 4, 5 3, 3, 4, 5 4, 3, 4, 5 5, 3, 4, 5 6, 3, 4, 5 7, 3, 4, 5 8, 3, 4, 5 9, 3, 4, 5 10, 3, 4, 5