From toth@dene@ @ending from kogentum@hu  Thu May  3 17:10:41 2018
From: toth@dene@ @ending from kogentum@hu (=?UTF-8?B?VMOzdGggRMOpbmVz?=)
Date: Thu, 3 May 2018 17:10:41 +0200
Subject: [Rd] length of `...`
In-Reply-To: <CAF8bMcaRW88VdyXw-kmCa9dw-awBiNiU+j+nktLRACCj4grPdQ@mail.gmail.com>
References: <d0b68216-4922-2249-e5e6-1589345dc6d7@kogentum.hu>
 <CAF8bMcaRW88VdyXw-kmCa9dw-awBiNiU+j+nktLRACCj4grPdQ@mail.gmail.com>
Message-ID: <ee90e1bb-8a81-bfd0-f415-e2b0652a72e4@kogentum.hu>

Thank you Bill (and the R-Core Team), this is even more than what I 
thought of. I somehow missed this in the NEWS.

BTW, substitue(...()) is beautiful.


On 05/03/2018 05:01 PM, William Dunlap wrote:
> In R-3.5.0 you can use ...length():
>    > f <- function(..., n) ...length()
>    > f(stop("one"), stop("two"), stop("three"), n=7)
>    [1] 3
> 
> Prior to that substitute() is the way to go
>    > g <- function(..., n) length(substitute(...()))
>    > g(stop("one"), stop("two"), stop("three"), n=7)
>    [1] 3
> 
> R-3.5.0 also has the ...elt(n) function, which returns
> the evaluated n'th entry in ... , without evaluating the
> other ... entries.
>    > fn <- function(..., n) ...elt(n)
>    > fn(stop("one"), 3*5, stop("three"), n=2)
>    [1] 15
> 
> Prior to 3.5.0, eval the appropriate component of the output
> of substitute() in the appropriate environment:
>    > gn <- function(..., n) {
>    +   nthExpr <- substitute(...())[[n]]
>    +   eval(nthExpr, envir=parent.frame())
>    + }
>    > gn(stop("one"), environment(), stop("two"), n=2)
>    <environment: R_GlobalEnv>
> 
> 
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
> 
> On Thu, May 3, 2018 at 7:29 AM, D?nes T?th <toth.denes at kogentum.hu 
> <mailto:toth.denes at kogentum.hu>> wrote:
> 
>     Hi,
> 
> 
>     In some cases the number of arguments passed as ... must be
>     determined inside a function, without evaluating the arguments
>     themselves. I use the following construct:
> 
>     dotlength <- function(...) length(substitute(expression(...))) - 1L
> 
>     # Usage (returns 3):
>     dotlength(1, 4, something = undefined)
> 
>     How can I define a method for length() which could be called
>     directly on `...`? Or is it an intention to extend the base length()
>     function to accept ellipses?
> 
> 
>     Regards,
>     Denes
> 
>     ______________________________________________
>     R-devel at r-project.org <mailto:R-devel at r-project.org> mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel
>     <https://stat.ethz.ch/mailman/listinfo/r-devel>
> 
> 

-- 
Dr. T?th D?nes ?gyvezet?
Kogentum Kft.
Tel.: 06-30-2583723
Web: www.kogentum.hu




[Rd] length of `...`
=?UTF-8?B?VMOzdGggRMOpbmVz?=