ordered_dict creates an ordered dictionary.

ordered_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)
.$popitem(last = TRUE)
.$has(key)
.$keys()
.$values()
.$update(d)
.$clear()
.$size()
.$as_list()
.$print()
  • key: scalar character, environment or function

  • value: any R object, value of the item

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

  • d: an ordered_dict object

See also

Examples

d <- ordered_dict(list(apple = 5, orange = 10))
d$set("banana", 3)
d$get("apple")
#> [1] 5
d$as_list()  # the order the item is preserved
#> $apple
#> [1] 5
#> 
#> $orange
#> [1] 10
#> 
#> $banana
#> [1] 3
#> 
d$pop("orange")
#> [1] 10
d$as_list()  # "orange" is removed
#> $apple
#> [1] 5
#> 
#> $banana
#> [1] 3
#> 
d$set("orange", 3)$set("pear", 7)  # chain methods