From wilm.schumacher at gmail.com  Fri Oct 21 15:22:52 2016
From: wilm.schumacher at gmail.com (Wilm Schumacher)
Date: Fri, 21 Oct 2016 15:22:52 +0200
Subject: [Rd] anonymous function parsing bug?
In-Reply-To: <97dcb35c-9173-5fc6-a499-680a4be15a94@gmail.com>
References: <97dcb35c-9173-5fc6-a499-680a4be15a94@gmail.com>
Message-ID: <68134961-8933-ddff-e6b4-21929b6768f4@gmail.com>

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"
>



[Rd] anonymous function parsing bug?
Wilm Schumacher