Looking inside the curry function in R (reverse curry?)

Let's pretend that

library(functional)
f = function(x, p) { x^p }
f2 = Curry(f, p=2)

Is there any way to find out what p value was set only for f2?

+4
source share
1 answer

See if this is helpful. In essence, the argument p is transferred to the environment of the body of the Curry () function:

> body(f2)
do.call(FUN, c(.orig, list(...)))
> body(f2)[[1]]
do.call
> body(f2)[[3]]
c(.orig, list(...))
> body(f2)[[3]][[2]]
.orig
> eval(body(f2)[[3]][[2]])
Error in eval(expr, envir, enclos) : object '.orig' not found
> eval(body(f2)[[3]][[2]], environment(f2) )
$p
[1] 2

They can be used as BrodieG comments when programming an attack on a problem:

> environment(f2)$.orig
$p
[1] 2

> environment(f2)$.orig$p
[1] 2

To understand why I didn’t stumble upon this, first compare:

> ls( envir=environment(f2) )
[1] "FUN"
> ls( envir=environment(f2) ,all.names=TRUE)
[1] "..."   ".orig" "FUN"  

The function lsdisplays only elements whose initial characters are not “dots” if the parameter is all.namesnot set to TRUE.

Thus, this is also indicative:

> environment(f2) $FUN
function(x, p) { x^p }
+5
source

Source: https://habr.com/ru/post/1570361/


All Articles