Rev 78987 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
---title: "Simplified Anonymous Function Notation"author:- Luke Tierneyoutput:html_document---All functions in R are anonymous: they do not need to be bound to avariable. If a function does happen to be bound to one or morevariables, there is no way to recover the names of those variables from afunction object.Functions in R are written and printed using the syntax```rfunction(x) x + 1```It is not uncommon to see comments from new users that this is a lotto write, especially for very simple functions like this one. It ishard to see that _writing_ is really a significant issue: Anyreasonable IDE/editor either already has a shortcut or allows one tobe created. The impact on code readability may be another matter.Functional programming expressions in particular, like```rlapply(y, function(t) is.null(t) || anyNA(t))```or```rReduce(function(u, v) u + 1 / v, x, right = TRUE)```might be more readable with a more concise notation.The tidyverse has introduced a formula-based notation. The `gsubfun`package uses another formula-based approach. And there are probablyothers. But these only work within the functions for which they aredesigned.An alternative worth exploring is for base R to support a concisesyntax in the parser. Ideally such a syntax should be able to expressany function, allowing for multiple arguments, default arguments, and`...`, though it would be most useful for simple functions.Three possibilities we have considered that were mentioned in my _useR2020_ keynote are<!-- From Tomas:https://en.wikipedia.org/wiki/Anonymous_function-->1. `(x) => x + 1`, `(x, y) => x + y` ([Javascript](https://en.wikipedia.org/wiki/Anonymous_function#JavaScript); similar to [Julia](https://en.wikipedia.org/wiki/Anonymous_function#Julia)).2. `\(x) x + 1`, `\(x, y) x + y` (similar to [Haskell](https://en.wikipedia.org/wiki/Anonymous_function#Haskell); `\` simulates $\lambda$).3. `@(x) x + 1`, `@(x, y) x + y` ([Matlab, Octave](https://en.wikipedia.org/wiki/Anonymous_function#MATLAB,_Octave)).All three have been implemented in the [`R-syntax` subversionbranch](https://svn.r-project.org/R/branches/R-syntax).The second and third are the most concise, perhaps too concise.But they require only minimal parser changes.The first option requires more extensive parser changes. My firstattempt was not good, but Andrew Craig modified the `R` 3.6.3 `gram.y`to make (x) => x + 1 work([tweet](https://twitter.com/andrew_cb2/status/1282969147366760449), [Tokyo.R talk](https://speakerdeck.com/andycraig/x-equals-x-plus-1),[code on GitHub](https://github.com/andycraig/r-anonymous-functions)),and I ported his changes to the current parser. These changes do seemto work, but my concern is that they may impose too great a burden onmaintaining the parser.All of these are only syntactic sugar for the longer version using `function`.The parsed AST is the same:```r> quote((x) => x + 1)function(x) x + 1> quote(\(x) x + 1)function(x) x + 1> quote(@(x) x + 1)function(x) x + 1```Parse data and source references do reflect the way the expressionswere entered. This is analogous to the way right assignment `->` ishandled:```r> quote(1 -> x)x <- 1```Editor support, e.g. in RStudio or ESS, and code formatting tools,e.g. `lintr` and `styler` would need to be updated for the new surfacesyntax. One drawback of the backslash notation is that it will makethese updates more error-prone.