[Rd] anonymous function parsing bug?

Wilm Schumacher wilm.schumacher at gmail.com
Fri Oct 21 15:22:52 CEST 2016


Hi,

sry for the double posting. I forgot to mention that this example

###
f<-function(x) {
     return( 2*x )
}(2)

class(f)

f(3)

f<-function(x) {
     return( 2*x )
}(4)(5)

f(6)
###

leads to

##
 > f<-function(x) {
+     return( 2*x )
+ }(2)
 >
 > class(f)
[1] "function"
 >
 > f(3)
[1] 6
 >
 > f<-function(x) {
+     return( 2*x )
+ }(4)(5)
 >
 > f(6)
[1] 12
##

which is even stranger (at least for me) and contradicts the first 
listing imho in behaviour.

Best wishes,

Wilm

Am 21.10.2016 um 15:10 schrieb Wilm Schumacher:
> Hi,
>
> I hope this is the correct list for my question. I found a wired 
> behaviour of my R installation on the evaluation of anonymous functions.
>
> minimal working example
>
> ###
> f<-function(x) {
>     print( 2*x )
> }(2)
>
> class(f)
>
> f(3)
>
> f<-function(x) {
>     print( 2*x )
> }(4)(5)
>
> f(6)
> ###
>
> leads to
>
> ###
> > f<-function(x) {
> + print( 2*x )
> + }(2)
> >
> > class(f)
> [1] "function"
> >
> > f(3)
> [1] 6
> Error in f(3) : attempt to apply non-function
> >
> > f<-function(x) {
> + print( 2*x )
> + }(4)(5)
> >
> > f(6)
> [1] 12
> Error in f(6) : attempt to apply non-function
>
> ###
>
> is this a bug or desired behavior? Using parenthesis of coures solves 
> the problem. However, I think the operator precedence could be the 
> problem here. I looked at the "./src/main/gram.y" and I think that the 
> line 385
>     |    FUNCTION '(' formlist ')' cr expr_or_assign %prec LOW
> should be of way higher precedence. But I cannot forsee the side 
> effects of that (which could be horrible in that case).
>
> If this is the desired behaviour and not a bug, I'm very interested in 
> the rational behind that.
>
> Best wishes,
>
> Wilm
>
> ps:
>
> $ R --version
> R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
>



More information about the R-devel mailing list