For some reasons, I'd like to play with R calls (at least as far as syntax is concerned) in a more Lisp/Scheme-like fashion (we all know that R has been heavily inspired by Scheme).
Thus, I set up the following function:
. <- function(f, ...)
eval(match.call()[-1], envir=parent.frame())
Which allows me to express e.g. the following R code:
x <- sort(sample(1:10, 5, replace=TRUE))
for (i in x) {
print(1:i)
}
in the following semantically equivalent form:
.(`<-`, x,
.(sort,
.(sample,
.(`:`, 1, 5),
5, replace=TRUE)))
.(`for`, i, x,
.(`{`,
.(print,
.(`:`, 1, i))))
I'm quite satisfied with the current definition of .
(as it's just made for fun). But it's surely far from perfect. In particular its performance is of course poor:
microbenchmark::microbenchmark(1:10, .(`:`, 1, 10))
## Unit: nanoseconds
## expr min lq median uq max neval
## 1:10 189 212.0 271.5 349 943 100
## .(`:`, 1, 10) 8809 10134.5 10763.0 11467 44066 100
So I wonder if you could come up with some ideas concerning the definition of .
that would address the above issue. C/C++ code is welcome.
do.call
and%>%
from themagrittr
package. The former is similar to your.
function (but takes the subsequent arguments as a list, not a separate arguments). The latter does much with manipulation of calls to make complete function calls and may have some techniques for getting environments and efficiency right. – Extortioner