x <- cyc_len(3)
y <- cyc_len(5)
xfun <- as.function(x)
yfun <- as.function(y)
stopifnot(xfun(yfun(2)) == as.function(y*x)(2)) # note transposition of x & y
# postfix notation has the very appealing result x(fg) == (xf)g
# Carets are good too, in that x^(fg) == (x^f)^g.
a <- rperm()
b <- rperm()
stopifnot(2^(a*b) == (2^a)^b)
# it's fully vectorized:
as.function(rperm(10,9))(1)
as.function(as.cycle(1:9))(sample(9))
as.function(allcyc(5:8))(1:6)
# standard recycling rules apply:
f <- as.function(allperms(3))
all(f(1:3) == f(c(1:3,1:3)))
Run the code above in your browser using DataLab