From Mark.Bravington at csiro.au  Sat Mar  7 01:43:51 2009
From: Mark.Bravington at csiro.au (Mark.Bravington at csiro.au)
Date: Sat, 7 Mar 2009 11:43:51 +1100
Subject: [Rd] question
In-Reply-To: <971536df0903051425h31070a46h76fc666b35fc3d78@mail.gmail.com>
References: <50d1c22d0903050622s2f450835k3edeb8c7f8f4df01@mail.gmail.com>,
	<971536df0903051425h31070a46h76fc666b35fc3d78@mail.gmail.com>
Message-ID: <62C82B39B8A85E4B95A18F7F7B852F87051705E41B@exvic-mbx03.nexus.csiro.au>

[ivo welch wrote:]

> The syntax for returning multiple arguments does not strike me as
> particularly appealing.  would it not possible to allow syntax like:
>
>   f= function() { return( rnorm(10), rnorm(20) ) }
>   (a,d$b) = f()
>


FWIW, my own solution is to define a "multi-assign operator":

'%<-%' <- function( a, b){
  # a must be of the form '{thing1;thing2;...}'
  a <- as.list( substitute( a))[-1]
  e <- sys.parent()
  stopifnot( length( b) == length( a))
  for( i in seq_along( a))
    eval( call( '<-', a[[ i]], b[[i]]), envir=e)
  NULL
}

Then I can write 

{a;d$b} %<-% f()

Actually it should probably return b invisibly, so that it can be chained a la {c$e$f;g$h} %<-% {a;d$b} %<-% f()

I haven't checked it exhaustively but it has done the job OK for me.

The name '%<-%' does already feature in one R package, can't remember which but it's to do with graph theory, so you might be better off calling it something else. I use the synonym %:=% which is closer to what I think R should have called its assignment operator in the first place ;)

HTH

Mark Bravington
CSIRO
Hobart
Australia



________________________________________
From: r-devel-bounces at r-project.org [r-devel-bounces at r-project.org] On Behalf Of Gabor Grothendieck [ggrothendieck at gmail.com]
Sent: 06 March 2009 09:25
To: ivo welch
Cc: r-devel at r-project.org
Subject: Re: [Rd] question

I posted this a few years ago (but found I never really had a
need for it):

http://tolstoy.newcastle.edu.au/R/help/04/06/1430.html



On Thu, Mar 5, 2009 at 9:22 AM, ivo welch <ivowel at gmail.com> wrote:
> dear R developers:  it is of course easy for a third party to make
> suggestions if this third party is both clueless and does not put in
> any work.  with these caveats, let me suggest something.
>
> The syntax for returning multiple arguments does not strike me as
> particularly appealing.  would it not possible to allow syntax like:
>
>   f= function() { return( rnorm(10), rnorm(20) ) }
>   (a,d$b) = f()
>
> this would just hide the list conversion and unconversion.  yes, I
> know how to accomplish this with lists, but it does not seem pretty or
> natural.
>
> regards,
>
> /ivo
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] question
Mark.Bravington at csiro.au