4 ms·
This is very interesting. Can you recommend some readings about these features? Especially as they may pertain to R.
by clircle 6y ago
This is very interesting. Can you recommend some readings about these features? Especially as they may pertain to R.
- nerdponx 6y agoThe "Metaprogramming" section of Advanced R by Hadley Wickham is a good introduction, although it emphasizes the author's own library and their own (intelligent but opinionated) approach, rather than the core language primitives: https://adv-r.hadley.nz/metaprogramming.html https://adv-r.hadley.nz/metaprogramming.html There is also Metaprogramming in R by Thomas Mailund, which seems good from the preview material I was able to find: https://www.apress.com/gp/book/9781484228807 https://www.apress.com/gp/book/9781484228807 For really powerful demonstrations of what R metaprogramming can do, see the Dplyr and Data.table packages. Personally, I've used it to implement things like shorthand lambdas, the Clojure threading macro, and a function composition operator that constructs and evaluates an expression of nested calls to keep runtime overhead down to a minimum. R macros are sometimes more powerful than true Lisp macros, in that there is no separation between the macro expansion phase and code execution. This is possible because R function arguments are lazily evaluated. The argument is a special "code object" that represents an expression. If you want to capture or change that expression, you can do so freely inside the function. So there is no need for macros, just functions that can capture unevaluated expressions. Incidentally this also makes R very hard to optimize, since any function can do this at any time.