dict: Dictionary

View source: R/dict.R

dictR Documentation

Dictionary

Description

dict creates an ordinary (unordered) dictionary (a.k.a. hash).

Usage

dict(items = NULL, keys = NULL)

Arguments

items

a list of items

keys

a list of keys, use names(items) if NULL

Details

Following methods are exposed:

.$set(key, value)
.$get(key, default)
.$remove(key, silent = FALSE)
.$pop(key, default)
.$has(key)
.$keys()
.$values()
.$update(d)
.$clear()
.$size()
.$as_list()
.$print()
  • key: a scalar character, an atomic vector, an enviroment or a function

  • value: any R object, value of the item

  • default: optional, the default value of an item if the key is not found

  • d: a dict object

See Also

ordered_dict

Examples

d <- dict(list(apple = 5, orange = 10))
d$set("banana", 3)
d$get("apple")
d$as_list()  # unordered
d$pop("orange")
d$as_list()  # "orange" is removed
d$set("orange", 3)$set("pear", 7)  # chain methods

# vector indexing
d$set(c(1L, 2L), 3)$set(LETTERS, 26)
d$get(c(1L, 2L))  # 3
d$get(LETTERS)  # 26

# object indexing
e <- new.env()
d$set(sum, 1)$set(e, 2)
d$get(sum)  # 1
d$get(e)  # 2

collections documentation built on Jan. 6, 2023, 1:25 a.m.