RobinHankin / disordR

https://robinhankin.github.io/disordR/
1 stars 0 forks source link

which() #46

Closed RobinHankin closed 1 year ago

RobinHankin commented 1 year ago

Function which() breaks the discipline:

> a <- disord(c(15,3,4,2,6))
> a
A disord object with hash 861a8c21a2410ba14a084f8f045e98a072694556 and elements
[1] 15  3  4  2  6
(in some order)
> which(a==4)
[1] 3
> 
RobinHankin commented 1 year ago

But it is possible to furnish a disord method for which():

a <- disord(c(15,3,4,2,6))
x <- disord(which(elements(a) > 3),h=hash(a))
x
A disord object with hash 861a8c21a2410ba14a084f8f045e98a072694556 and elements
[1] 1 3 5
(in some order)

Now object x does not make any sense on its own because it specifies that a[1] and a[3] and a[5] are special, and of course the numbers 1,3,5 are implementation-specific. But x does make sense as an argument to square brackets:

a[x]
15 4 6 
a[x] <- 100
a
A disord object with hash 861a8c21a2410ba14a084f8f045e98a072694556 and elements
[1] 100   3 100   2 100
(in some order)
> 

[and in particular, if only a single element of the argument is TRUE, then this can be passed to [[() or indeed [[<-() without breaking disordR discipline; see issue #17]. So perhaps we don't want to throw the baby out with the bathwater. The way forward is to create a new S4 class disindex for x which can only be used for indexing purposes.

One confusing aspect is that x as defined above is a disord object, but the order of the elements of x is not really important, in the sense that 1 3 5 and 3 5 1 would be the "same" object; and this ambiguity is dealt with perfectly well by the disord class. In this context, it is the elements themselves that are implementation-specific: x might be 2 4 5 on a different day. So, somehow we want to allow x to exist, linked to a [via the hash code], but the only thing we are allowed to do with x is to use it in a[x] or a[x] <- value (or perhaps a[[x]] in list extraction). I am not even sure if printing it should be allowed.

But x should definitely not be a disord object: max(x), for example, is implementation-specific and should be disallowed.

RobinHankin commented 1 year ago

reopening pending proper integration of ae58028 (i.e. a nice vignette and .Rd files).