[Rd] Bug with ..0

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 31 00:20:00 CEST 2010


On Sun, May 30, 2010 at 4:31 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> As far as I know, there is none. It is reserved simply because it follows
> the pattern of "..n". It would not be hard to make ..0 specially unreserved,
> but what would be the point?

Here is the case I had in mind.

The gsubfn function in the gsubfn package is like gsub except the
replacement string can be a replacement function such that it passes
the back references in the pattern as successive args to the function.
Here it would pass two args to the function represented by (.) and (.)
in the regular expression and then replace the match with the output
of the function which in this case is the sum of the digits surrounded
with angle brackets:

> gsubfn("(.)#(.)", function(...) paste0("<", as.numeric(..1) + as.numeric(..2), ">"), "1#2 3#4")
[1] "<3> <7>"

There is also an option to pass the entire match followed by the back
references to the user function.  In that case it would be nice for
the user to be able to write the user function as follows where ..0
means the entire match.  Here we replace the match with an angle
bracket, the entire match, an equal sign, the sum and an end angle
bracket:

> gsubfn("(.)#(.)", function(..0, ...) paste0("<", ..0, "=", as.numeric(..1) + as.numeric(..2), ">"), "1#2 3#4")
[1] "<1#2=3> <3#4=7>"

Note the use of ..0 to mean the entire match is in this context and
how natural it is.

To do the above the best would be if we could just use ..0 as an
ordinary variable.  Barring that we could replace all occurrences of
..0 with X..0, say, so that:

   function(..0, ...) list(..0, ..1)

would be transformed to the function

   function(X..0, ...) list(X..0, ..1)

and that could work now but that would depend on R not being changed
in the future to issue an error when a function such as f above that
uses ..0 is created.

(Although not illustrated here there is also a formula notation which
allows one to specify the function as a formula whose body is taken to
be the formula's RHS and the args are reconstructed from the RHS free
variables.   In that case the ..0 notation becomes even more useful.)



More information about the R-devel mailing list