Title: | R6 Dictionary Interface |
---|---|
Description: | Efficient object-oriented R6 dictionary capable of holding objects of any class, including R6. Typed and untyped dictionaries are provided as well as the 'usual' dictionary methods that are available in other OOP languages, for example listing keys, items, values, and methods to get/set these. |
Authors: | Raphael Sonabend [aut, cre] |
Maintainer: | Raphael Sonabend <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2024-11-19 05:56:22 UTC |
Source: | https://github.com/xoopr/dictionar6 |
Sugar function wrapping [Dictionary]$new
.
dct(..., x = list(...), types = NULL)
dct(..., x = list(...), types = NULL)
... |
( |
x |
( |
types |
( |
# untyped dct(a = 1, b = 2, c = "a") # typed - class is forced dct(a = 1L, b = 2L, c = 3L, types = "integer") # list constructor dct(x = list(a = 1, b = 2, c = "a")) # with R6 d <- dct(a = 1) dct(d = d)
# untyped dct(a = 1, b = 2, c = "a") # typed - class is forced dct(a = 1L, b = 2L, c = 3L, types = "integer") # list constructor dct(x = list(a = 1, b = 2, c = "a")) # with R6 d <- dct(a = 1) dct(d = d)
Dictionaries are essential objects in other object-oriented languages, such as Python. The primary function of a dictionary is to store items in a key-value pair. Where the key is the name of the item and the value is the value. This object contains all the 'usual' methods that would be found in other languages, including setting and getting values, adding and removing items, and containedness checks.
keys
None -> character()
Get dictionary keys.
values
None -> list()
Get dictionary values.
items
list() -> self
/ None -> list()
If x
is missing then returns the dictionary items.
If x
is not missing then used to set items in the dictionary.
length
None -> integer(1)
Get dictionary length as number of items.
typed
None -> logical(1)
Get if the dictionary is typed (TRUE
) or not (FALSE
).
types
None -> character()
Get the dictionary types (NULL if un-typed).
new()
Constructs a Dictionary
object.
Dictionary$new(..., x = list(...), types = NULL)
...
(ANY
)
Named arguments with names corresponding to the items to add to the
dictionary, where the keys are the names and the values are the
elements. Names must be unique.
x
(list()
)
A named list with the names corresponding to the items to add to the
dictionary, where the keys are the list names and the values are the
list elements. Names must be unique.
types
(character()
)
If non-NULL then types
creates a typed dictionary in which all
elements of the dictionary must inherit from these types
. Any class
can be given to types
as long as there is a valid as.character
method associated with the class.
add()
Add new items to the dictionary.
Dictionary$add(x = list(), keys = NULL, values = NULL)
x
(list()
)
Same as initialize, items to add to the list.
keys
(character()
)
If x
is NULL then keys
and values
can be provided to add the
new items by a character vector of keys and list of values instead.
values
(list()
)
If x
is NULL then keys
and values
can be provided to add the
new items by a list of keys and values instead.
rekey()
Change the name of a given key.
Dictionary$rekey(key, new_key)
key
(character(1)
)
Key to rename.
new_key
(character(1)
)
New name of key, must not already exist in dictionary.
revalue()
Change the value of a given item.
Dictionary$revalue(key, new_value)
key
(character(1)
)
Key of item to revalue.
new_value
(character(1)
)
New value of item.
remove()
Removes the given item from the list.
Dictionary$remove(key)
key
(character(1)
)
Key of item to remove.
get()
Gets the given items from the dictionary. If only one item is requested then returns the (unlisted) item, or if multiple items are requested as the dictionary is typed, then the unlisted items are returned.
Dictionary$get(keys, clone = TRUE)
keys
(character()
)
Keys of items to get.
clone
(logical(1)
)
If TRUE
(default) then deep clones R6 objects if requested.
get_list()
Gets the given items from the dictionary as list.
Dictionary$get_list(keys, clone = TRUE)
keys
(character()
)
Keys of items to get.
clone
(logical(1)
)
If TRUE
(default) then deep clones R6 objects if requested.
has()
Checks if the given key is in the list, returns a logical.
Dictionary$has(key)
key
(character(1)
)
Key to check.
assert_contains()
Asserts if the given keys are in the list, returns keys invisibly if assertion passes otherwise errors.
Dictionary$assert_contains(keys)
keys
(character()
)
Keys to check.
has_value()
Checks if the given value is in the list, returns a logical.
Dictionary$has_value(value)
value
(ANY
)
Value to check.
print()
Prints dictionary.
Dictionary$print(n = 2)
n
(integer(1)
)
Number of items to print on either side of ellipsis.
summary()
Summarises dictionary.
Dictionary$summary(n = 2)
n
(integer(1)
)
Number of items to print on either side of ellipsis.
merge()
Merges another dictionary, or list of dictionaries, into self.
Dictionary$merge(x)
x
(Dictionary(1) | list()
)
Dictionary or list of dictionaries to merge in, must have unique keys.
clone()
The objects of this class are cloneable with this method.
Dictionary$clone(deep = FALSE)
deep
Whether to make a deep clone.