[Rd] External special functions (SPECIALSXP)

2011-05-25 Thread Peter Danenberg
Is it possible to define an external special function (SPECIALSXP)?
I'm trying to do some language-level work, and don't want my arguments
evaluated before they hit C.

It looks like the only way to define a SPECIALSXP is by using XX0 in
the `eval' field of R_FunTab; is there any way to make this applicable
to externally defined functions?

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


Re: [Rd] External special functions (SPECIALSXP)

2011-05-25 Thread Duncan Murdoch

On 25/05/2011 1:10 PM, Peter Danenberg wrote:

Is it possible to define an external special function (SPECIALSXP)?
I'm trying to do some language-level work, and don't want my arguments
evaluated before they hit C.

It looks like the only way to define a SPECIALSXP is by using XX0 in
the `eval' field of R_FunTab; is there any way to make this applicable
to externally defined functions?



I don't think so.  However, if you don't want to evaluate the arguments, 
just pass substitute(arg) to your function instead of arg.  For example,


 f <- function(a, b) {
   print(substitute(b))
   print(substitute(a))
 }

f( x <- 1, y <- 2 )

will print but never evaluate the assignments.

Duncan Murdoch

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


Re: [Rd] External special functions (SPECIALSXP)

2011-05-25 Thread Peter Danenberg
> However, if you don't want to evaluate the arguments, just pass
> substitute(arg) to your function instead of arg.

Thanks, Duncan; the problem is, I'm trying to substitute on `...' and
I don't think I can access `...' without inadvertently evaluating it.

I'm trying to write a debugging function which, given any number of
expressions, prints the expression next to its evaluation.

This is trivial to do in the single arity case:

  debug <- function(expression) {
cat(substitute(expression), expression, "\n")
  }

  a <- 2
  debug(a)

  a 2

but I'm not sure how to make it work with variable arity without
resorting to SPECIALSXP.

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


Re: [Rd] External special functions (SPECIALSXP)

2011-05-25 Thread William Dunlap
> f <- function(...) {
+ dotArgList <- substitute(list(...))
+ dotArgList
+ }
> f(cat("foo\n"), stop("Oops"), warning("Hmm"))
list(cat("foo\n"), stop("Oops"), warning("Hmm"))
> # i.e., no argument was evaluated

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -Original Message-
> From: r-devel-boun...@r-project.org 
> [mailto:r-devel-boun...@r-project.org] On Behalf Of Peter Danenberg
> Sent: Wednesday, May 25, 2011 11:06 AM
> To: Duncan Murdoch
> Cc: r-devel@r-project.org
> Subject: Re: [Rd] External special functions (SPECIALSXP)
> 
> > However, if you don't want to evaluate the arguments, just pass
> > substitute(arg) to your function instead of arg.
> 
> Thanks, Duncan; the problem is, I'm trying to substitute on `...' and
> I don't think I can access `...' without inadvertently evaluating it.
> 
> I'm trying to write a debugging function which, given any number of
> expressions, prints the expression next to its evaluation.
> 
> This is trivial to do in the single arity case:
> 
>   debug <- function(expression) {
> cat(substitute(expression), expression, "\n")
>   }
> 
>   a <- 2
>   debug(a)
> 
>   a 2
> 
> but I'm not sure how to make it work with variable arity without
> resorting to SPECIALSXP.
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

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


Re: [Rd] External special functions (SPECIALSXP)

2011-05-25 Thread William Dunlap


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -Original Message-
> From: William Dunlap 
> Sent: Wednesday, May 25, 2011 11:20 AM
> To: 'Peter Danenberg'
> Cc: r-devel@r-project.org
> Subject: RE: [Rd] External special functions (SPECIALSXP)
> 
> > f <- function(...) {
> + dotArgList <- substitute(list(...))
> + dotArgList
> + }
> > f(cat("foo\n"), stop("Oops"), warning("Hmm"))
> list(cat("foo\n"), stop("Oops"), warning("Hmm"))
> > # i.e., no argument was evaluated

Or, if you'd prefer a pairlist over a call to list:
  > fp <- function(...) {
  + dotArgList <- substitute(...())
  + dotArgList
  + }
  > fp(message=cat("foo\n"), error=stop("Oops"),
possibleProblem=warning("Hmm"))
  $message
  cat("foo\n")

  $error
  stop("Oops")
  
  $possibleProblem
  warning("Hmm")

  > class(.Last.value)
  [1] "pairlist"
I would not have thought of this syntax myself but have
seen it in other folks' code.

> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com  
> 
> > -Original Message-
> > From: r-devel-boun...@r-project.org 
> > [mailto:r-devel-boun...@r-project.org] On Behalf Of Peter Danenberg
> > Sent: Wednesday, May 25, 2011 11:06 AM
> > To: Duncan Murdoch
> > Cc: r-devel@r-project.org
> > Subject: Re: [Rd] External special functions (SPECIALSXP)
> > 
> > > However, if you don't want to evaluate the arguments, just pass
> > > substitute(arg) to your function instead of arg.
> > 
> > Thanks, Duncan; the problem is, I'm trying to substitute on 
> `...' and
> > I don't think I can access `...' without inadvertently 
> evaluating it.
> > 
> > I'm trying to write a debugging function which, given any number of
> > expressions, prints the expression next to its evaluation.
> > 
> > This is trivial to do in the single arity case:
> > 
> >   debug <- function(expression) {
> > cat(substitute(expression), expression, "\n")
> >   }
> > 
> >   a <- 2
> >   debug(a)
> > 
> >   a 2
> > 
> > but I'm not sure how to make it work with variable arity without
> > resorting to SPECIALSXP.
> > 
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> > 

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


Re: [Rd] External special functions (SPECIALSXP)

2011-05-25 Thread Duncan Murdoch

On 25/05/2011 2:05 PM, Peter Danenberg wrote:

>  However, if you don't want to evaluate the arguments, just pass
>  substitute(arg) to your function instead of arg.

Thanks, Duncan; the problem is, I'm trying to substitute on `...' and
I don't think I can access `...' without inadvertently evaluating it.

I'm trying to write a debugging function which, given any number of
expressions, prints the expression next to its evaluation.

This is trivial to do in the single arity case:

   debug<- function(expression) {
 cat(substitute(expression), expression, "\n")
   }

   a<- 2
   debug(a)

   a 2

but I'm not sure how to make it work with variable arity without
resorting to SPECIALSXP.


I suppose you can do a clunky workaround like this:

  debug <- function(e1, e2, sentinel) {
e1 <- if(!missing(e1)) substitute(e1)
e2 <- if(!missing(e2)) substitute(e2)
if (!missing(sentinel)) stop("this function only handles 2 
expressions")

print(e1)
print(e2)
}

(but use some finite limit that's more than 2).

Duncan Murdoch

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


Re: [Rd] External special functions (SPECIALSXP)

2011-05-25 Thread Peter Danenberg
Quoth William Dunlap on Setting Orange, the 72nd of Discord:
> > f <- function(...) {
> + dotArgList <- substitute(list(...))
> + dotArgList
> + }
> > f(cat("foo\n"), stop("Oops"), warning("Hmm"))
> list(cat("foo\n"), stop("Oops"), warning("Hmm"))
> > # i.e., no argument was evaluated

Thanks, William! Something like this works perfectly:

  > debug <- function(...) {
  +   promises <- as.list(substitute(list(...)))[-1]
  +   str(structure(Map(eval, promises),
  + names=Map(deparse, promises)))
  + }
  >
  > a <- 2
  > debug(a, a + a)
  List of 2
   $ a: num 2
   $ a + a: num 4
  >

For some reason, I had tried every permutation of `substitute', `list'
and `...' except that one; coming to the erroneous conclusion that I
couldn't avoid eval-ing `...'.

I may have to screw around with the evaluation environment; but,
otherwise, this is fantastic!

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


Re: [Rd] Curry: proposed new functional programming, er, function.

2011-05-25 Thread Peter Danenberg
Sharpie wrote:
> Currently, the only Curry implementation I know of is in the roxygen
> package which is kind of a weird dependency to install just for this
> one function.

I end up using Curry so much outside of Roxygen that I spun it off
into the `functional' package:

  https://r-forge.r-project.org/projects/functional/

Compose is there, too, and a couple other superfluous things.

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