From wdunlap at tibco.com  Sun Feb 17 06:47:45 2013
From: wdunlap at tibco.com (William Dunlap)
Date: Sun, 17 Feb 2013 05:47:45 +0000
Subject: [Rd] Printing of anonymous functions in calls is sub-optimal
In-Reply-To: <CABdHhvGsqp76OZYLm12R9AvrekY5KnL3tOwPHpSnc0c2=DPyvQ@mail.gmail.com>
References: <CABdHhvEgK1g4gYhmovu_EbvSN185g9LnLAacC5ojWup3_aG07w@mail.gmail.com>
	<511FA362.9060305@gmail.com>
	<CABdHhvGsqp76OZYLm12R9AvrekY5KnL3tOwPHpSnc0c2=DPyvQ@mail.gmail.com>
Message-ID: <E66794E69CFDE04D9A70842786030B931B90370C@PA-MBX04.na.tibco.com>

> I suspect it's only when you have a function in the quoted call, not a symbol:

Add a call to 'function' (as opposed to the function made by evaluating that call)
to your test suite:

  > Q <- list(
         q1 = quote(getFunction("-")(x)),
         q2 = substitute(f(x), list(f = function(x) {-x})),
         q3 = substitute(f(x), list(f = quote(function(x) {-x}))))
  > sapply(Q, function(x)class(x[[1]]))
          q1         q2         q3 
      "call" "function"     "call" 
  > Q
  $q1
  getFunction("-")(x)
  
  $q2
  function (x) 
  {
      -x
  }(x)

  $q3
  function(x) {
      -x
  }(x)

  > sapply(Q, eval, list(x=137))
    q1   q2   q3 
  -137 -137 -137

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf
> Of Hadley Wickham
> Sent: Saturday, February 16, 2013 7:22 AM
> To: Duncan Murdoch
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] Printing of anonymous functions in calls is sub-optimal
> 
> > This is a little tricky for the deparser.  It sees a call to a function
> > which was determined by an expression.  Sometimes you want parens, sometimes
> > you don't.  For example, if getfun(y) returns a function, it's clearer to
> > display a call as getfun(y)(x) than (getfun(y))(x).
> >
> > I'll see if I can work out which kinds of expressions need to be
> > parenthesized and implement it in the deparser.
> 
> I suspect it's only when you have a function in the quoted call, not a symbol:
> 
> # Don't add parens
> q1 <- quote(g(f)(x))
> is.symbol(q1[[1]])
> 
> # Add parents
> q2 <- substitute(f(x), list(f = function(x) {x + 1}))
> is.function(q2[[1]])
> 
> Thanks for thinking about it!
> 
> Hadley
> 
> --
> Chief Scientist, RStudio
> http://had.co.nz/
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Printing of anonymous functions in calls is sub-optimal
William Dunlap