mark.braving...@csiro.au 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
> }
>   

you might want to have the check less stringent, so that rhs may consist
of more values that the lhs has variables.  or even skip the check and
assign NULL to a[i] for i > length(b).  another idea is to allow %<-% to
be used with just one variable on the lhs.

here's a modified version:

    '%<-%' <- function(a, b){
        a <- as.list( substitute(a))
        if (length(a) > 1)
            a <- a[-1]
        if (length(a) > length(b))
            b <- c(b, rep(list(NULL), length(a) - length(b)))
        e <- sys.parent()
        for( i in seq_along( a))
            eval( call( '<-', a[[ i]], b[[i]]), envir=e)
        NULL }

    {a; b} %<-% 1:2
    # a = 1; b = 2
    a %<-% 3:4
    # a = 3
    {a; b} %<-% 5
    # a = 5; b = NULL


vQ

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

Reply via email to