[R] chaining closure arguments on-the-fly

Benjamin Tyner btyner @end|ng |rom gm@||@com
Sat Jun 20 22:44:38 CEST 2020


On 6/20/20 9:00 AM, Duncan Murdoch wrote:
> How about
>
> g <- function(x, y = x) {
>   f(x, y)
> }
> g(x = 3)
>
> or even
>
> yEqualsX <- function(f) function(x, y = x) f(x, y)
>
> yEqualsX(f)(x = 3)
>
> These are a lot like currying, but aren't currying, so they may be 
> acceptable to you.  Personally I'd choose the first one.
>
> Duncan Murdoch
>
Thank you Duncan; I should have been more explicit that I was also 
trying to avoid defining any new functions, but yes, it's hard to argue 
with the wrapper approach as a longstanding best practice.

Basically I'm wondering if it would be theoretically possible to 
construct a function "g" (possibly it would have to be 
primitive/internal) such that

    f(x = 3, y = g(expr))

would evaluate expr in the evaluation environment of f? After rereading 
section 4.3.3 of the R Language Definition, it's clear that supplied 
arguments are evaluated in the calling environment; though trickery in 
f()'s body may be used to evaluate elsewhere, such options seem limited 
from within the argument list itself.

Ben



More information about the R-help mailing list