RobinHankin / permutations

https://robinhankin.github.io/permutations/
5 stars 3 forks source link

as.function(id) #11

Closed RobinHankin closed 1 year ago

RobinHankin commented 5 years ago

This works:

f  <- as.function(as.word(id,3))
f(1:3)
[1] 1 2 3

But this doesn't:

f  <- as.function(id)
f(1:3)
Error in x[, a] : subscript out of bounds

I wonder whether it would be a good idea to include special dispensation in as.function.permutation() to check for its argument being the identity and, if so, return function(x){x}. Not sure whether this would break something.

RobinHankin commented 2 years ago

There is a more general bug. Look:

> as.function(as.cycle(1:6))(9)
Error in x[, a] : subscript out of bounds
> 

Above, I would expect the call to return 9 on the reasonable grounds that the cycle (123456) leaves numbers >=7 unchanged.

RobinHankin commented 2 years ago

There is a sort of fix:

 o <- as.word(as.cycle(1:2))
> as.function(o)(4)
Error in x[, a] : subscript out of bounds
> size(o) <- 6
> as.function(o)(4)
[1] 4
> 

The first call returns an error, but not the second. This should be documented.