RobinHankin / disordR

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

list extraction #17

Closed RobinHankin closed 1 year ago

RobinHankin commented 3 years ago

Coercing a list to a disord object allows one to abuse double-square brackets:

> a <- disord(list(6:7,3:9))
> a
A disord object with hash 44c1e2907fd0c650d42d794ea469e8b2b51e5a75 and elements
[[1]]
[1] 6 7

[[2]]
[1] 3 4 5 6 7 8 9

(in some order)
> a[[1]]
[1] 6 7
> 

Above, I would expect a[[1]] to return an error on the grounds that the order of the elements is implementation -specific.

RobinHankin commented 1 year ago

there is a similar problem with list extraction even for disords of numeric vectors:

> disord(c(8,1,2,3))[[2]]
[1] 1
> 

I need a general set of methods for [[.

RobinHankin commented 1 year ago

But the big big big problem is that lapply() is used on disordered lists in at least three packages (freealg, mvp, and stokes). It might be best to treat this as a documentation issue and point out that lapply() breaks disordR discipline.

RobinHankin commented 1 year ago

According to revdepcheck, three packages are broken by removing support for [[: freealg, mvp, and stokes. But the local version of stokes seems to work. Have already fixed freealg and mvp in https://github.com/RobinHankin/freealg/commit/22e41035f1094463eecdbb049713dce8090bfdf4 and https://github.com/RobinHankin/mvp/commit/bb81d4a3431ea2b91e564b274481ee3cefb8158e respectively, must remember to upload new versions to CRAN before finally resolving the disordR issue.

RobinHankin commented 1 year ago

Sometimes, idiom that works for list extraction has a natural interpretion for disord lists:

> x <- list(5:6,1:5,2:7)
> x[[which(lapply(x,length)>5)]]
[1] 2 3 4 5 6 7
> 

Now, this arguably should work for a disord list, but currently does not:

> x <- disord(x)
> x[[which(lapply(x,length)>5)]]
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'i' in selecting a method for function '[[': which() does not make sense for disord object
> 

Above, the gloss would be "extract the single element of x with length exceeding 5", which is disord-OK. But the problem is not with the list extraction; as the error messge says, the issue is which():

> which(lapply(x,length)>5)
Error in .local(x) : which() does not make sense for disord object
> 

It is not clear to me whether implementing this is (a), desirable or (b), possible. But in any event, current behaviour is not adequately documented.

RobinHankin commented 1 year ago

Closing, double square bracket extraction now covered by disind indexing