The proposed lambda syntax for R is `\(x) x+1` so `\` will just be shorthand for `function`.
The proposed lambda syntax for R is `\(x) x+1` so `\` will just be shorthand for `function`.
function(x) {x + 1}
is already logically equivalent to and from some perspectives an arguable syntax improvement on \(x) x + 1
Giving everyone two ways of doing one thing just means the tutorials will be fragmented and beginners even more confused.Tierney mentioned that tidyverse found function(x) too verbose and uses fomula syntax. Given how tidyverse often uses the "y ~ x" formula notation, this might actually be picking up deficiencies in R's macro system rather than in the function notation and the problem got misdagnosed.
\(x) x+1
instead of function(x) x+1
not only saves a few keystrokes.It will also produce shorter, and clearer, lines of code.
What I don’t understand is the reference to “formula syntax”. What is the issue and how does the new syntax solve it?
And all the rest of the comment I wouldn't have typed except I'm already replying, since I know this is one of those two-types-of-people-who-don't-change-opinions situations. But...
> not only saves a few keystrokes.
R is secretly a lisp. People can define whatever they want to be whatever they want. Pipes were already implemented in a library (try doing that in Python). Make your own library or bind \ to a keyboard macro or something if your fingers are on the point of crumbling under the stress of those 7 keystrokes.
Defaults using real words to describe things is good. The function to create a function being function() is eminently reasonable. \() is meaningless and about as useful as a one-word variable
> It will also produce shorter, and clearer, lines of code.
Opinons very much divide. Code length is only a proxy for load on a reader's short term memory which is what matters. \ is going to put more burden on someone if they aren't very familiar with R. Most R coders are not full time programmers and not very good at R.
[0] https://www.rdocumentation.org/packages/base/versions/3.6.2/... [1] https://www.rdocumentation.org/packages/stats/versions/3.6.2... [2] http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/
‘\(x) x + 1’ is parsed as ‘function(x) x + 1’
I also know that R has some vestigial scheme under the hood, but the syntax is not lisp (it was taken from S). In common lisp one could easily use a macro or reader macro but in R a change in the parser is needed so “\” can be used instead of “function”.Note that the existing “f <- function(...) ...” syntax is not being removed. But I write a lot of code like
state.range <- apply(state.x77, 2, function(x) c(min(x), median(x), max(x)))
and it will be an improvement to be able to condense it a bit to state.range <- apply(state.x77, 2, \(x) c(min(x), median(x), max(x)))