Usually all R's objects are immutable with copy-on-modify semantics. This is not true for environments
, which have reference semantics. Iterators in R (iterators package) are implemented using environments
and are mutable. This can be confusing. Consider following simple example:
library(iterators)
it1 <- iter(1:4)
it2 <- it1
nextElem(it1)
# 1
nextElem(it2)
# 2
And this is not what most of R users expect. The question is how to effectively make a copy of iterator? For the moment I have messy solution (borrowed this idea) for simple case above:
it1 <- iter(1:4)
it2 <- it1
it2$state <- as.environment(as.list(it1$state))
next_el(it1)
# 1
next_el(it2)
# 1
But I feel like I'm missing something, and it also doesn't look like general solution.