knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) library("disordR")
Ordinary R vectors are unsuitable for working with values of
associative maps because elements of an R vector may be accessed by
reference to their location in the vector, but associative maps are
stored in arbitrary order. However, when associating keys with values
one needs both parts to be in 1-1 correspondence, so one cannot
dispense with the order entirely. The disordR package includes a
single S4
class, disord
. This class allows one to perform only
those operations appropriate for manipulating values (or keys) of
associative maps.
A useful heuristic is that one is only allowed to access or modify a disord object using a python list comprehension. The idea is to prevent "illegal" operations on values (or keys) of associative maps, whose order is undefined or at best implementation-specific, while allowing sensible operations.
disord
package in useI will illustrate the package by a few examples of legal and illegal
code. First create a simple disord
object:
set.seed(0) a <- rdis() # a random disord object a
We may perform various operations on this object:
a+4 a > 5 a[a<6]
with no difficulty. But if we try to find elements of a
with a
particular offset or offsets, the system returns an error:
a[1] a[c(2,3)]
This is because the elements of a
are stored in an
implementation-specific order and there is no such thing as the
"first" element. We may manipulate elements of a
by reference to
their values but not by their position in the vector:
a[a<3] <- 0 # round small elements down a
Replacement methods can access subsets where this makes sense:
x <- disord(1:10) x x[x<3] <- x[x<3] + 100 x
disord
objectsIf we create another disord
object, b
:
b <- rdis() b
Then a
and b
have the same length, but adding them vectorially is
forbidden because the order of their elements is implementation-specific:
a+b
Also, replacement methods that access cross-referenced locations are forbidden:
a[b < 4] <- 5
(observe carefully that a[b<14] <- 5
is legal). However, sometimes
one knows that two disord objects have the same ordering (perhaps
a
is the key, and b
the value, of an associative array). In this
case one may cross-reference them provided that the hash codes of the
two objects agree:
a <- rdis() b <- disord(sample(9),hash(a)) a b
See how a
and b
have the same hash code. Then, although the
elements of a
and b
are stored in an implementation-specific (and
thus unknown) order, whatever order it is is the same in both objects
and they are relatable:
a+b a[b < 0.5] a[b < 0.2] <- b[b < 0.2] a
A disord object is comparable with python arrays with the proviso that one is only allowed to access or manipulate elements via list comprehensions. See the following python session:
~ % python3 Python 3.9.5 (default, May 4 2021, 03:33:11) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from numpy import * >>> a = random.rand(5) >>> b = random.rand(5) >>> a array([0.13275574, 0.07243463, 0.0543058 , 0.36633955, 0.73795801]) >>> b array([0.14934221, 0.11321413, 0.6135964 , 0.53558058, 0.6396702 ]) >>> [i**2 for i in a] [0.017624086607707354, 0.005246776214236293, 0.002949120389839953, 0.1342046637421116, 0.54458202262916] >>> [i+j for i,j in zip(a,b)] [0.28209795105056, 0.18564876373593042, 0.6679022004516395, 0.901920128499422, 1.377628206483919] >>>
Note that in the last line zip()
is used to relate a
and b
which
is accomplished in the package by explicitly setting the hash code.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.