title: R digest of closure changes after repeated call
body: <p>I was trying to memoise an R function that operates on a reasonably complicated closure and found that repeated calls to the memoised function would compute the result twice before retrieving the cached result, instead of only computing it on the first call.</p>
  
  <p>I think there's a few things going on that cause it, but this question focuses on the first problem I'm trying to understand.</p>
  
  <p>My closure is essentially a list of functions:</p>
  
  <pre><code>foo &lt;- function() {
    bar &lt;- function() NULL
    list(bar = bar)
  }
  </code></pre>
  
  <p>Creating an instance of foo, and running digest:</p>
  
  <pre><code>f &lt;- foo()
  digest::digest(f)
  [1] "5a54945202730e8c997aa41a27d23dd5"
  </code></pre>
  
  <p>Instantiating f again results in a different output for digest:</p>
  
  <pre><code>f &lt;- foo()
  digest::digest(f) # different hash
  [1] "180471cb687f12271c8ba7800d02695c"
  </code></pre>
  
  <p>But doing it a third time shows the result has "stabilised":</p>
  
  <pre><code>f &lt;- foo()
  digest::digest(f) # no change in hash
  [1] "180471cb687f12271c8ba7800d02695c"
  </code></pre>
  
  <p>I've been reading a lot about how environments work with closures (creating shared mutable state, limiting references visible in the enclosed environment etc.) but am still scratching my head at this seemingly simple problem! </p>
  
  <p>Why does digest produce different output on the second call?</p>