[Rd] How do I reliably and efficiently hash a function?
Jeroen Ooms
jeroenooms at gmail.com
Fri Dec 11 14:44:12 CET 2015
On Fri, Dec 11, 2015 at 12:49 AM, Konrad Rudolph
<konrad.rudolph+r-devel at gmail.com> wrote:
>
> On the chance that I’m trying to solve the wrong Y to an X/Y problem,
> the full context to the above problem is explained in [1]. In a
> nutshell, I am hooking a new environment into a function’s parent.env
> chain, by re-assigning the function’s `parent.env` (naughty, I know).
> This is done so that the function `f` finds objects defined inside
> that environment without having to attach it globally.
Not sure if this is helpful, but you can implement this more naturally
using closures without hacking on environments. As I understand it,
your functions have some shared state, and some private. So each
function needs a private parent env, which all share a common
grand-parent that holds your shared objects. Maybe this example helps:
new_closure <- (function(shared = 0){
function(name, priv = 0){
function(){
priv <<- priv +1
shared <<- shared +1
print(sprintf("Total:%d; %s:%d", shared, name, priv))
}
}
})()
fun1 <- new_closure("erik")
fun2 <- new_closure("anna")
fun1()
fun1()
fun1()
fun2()
fun1()
fun1()
fun2()
More information about the R-devel
mailing list