From jeroenooms at gmail.com  Fri Dec 11 14:44:12 2015
From: jeroenooms at gmail.com (Jeroen Ooms)
Date: Fri, 11 Dec 2015 14:44:12 +0100
Subject: [Rd] How do I reliably and efficiently hash a function?
In-Reply-To: <CAM2gKPbSZjLUpVW87iaKczF-2eDHiT6GReu28SBJ98mSt8Djcg@mail.gmail.com>
References: <CAM2gKPbSZjLUpVW87iaKczF-2eDHiT6GReu28SBJ98mSt8Djcg@mail.gmail.com>
Message-ID: <CABFfbXupq48SULO6AVMhPMucC5Jzc5oznz+HXUjyjEJkhwFA8Q@mail.gmail.com>

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()



[Rd] How do I reliably and efficiently hash a function?
Jeroen Ooms