copy: Copy a generator safely

Description Usage Arguments Examples

View source: R/copy.R

Description

Assigning a new name to a generator does not break the link with the underlying state. Calling the new generator will update the state in the previous too. This is an unsafe copy. To break this link, and spawn a new underlying state, use this function.

Usage

1
copy(.generator)

Arguments

.generator

A function with the class generator

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Unsafe copy
counter <- generator(0, ~ state + 1)
counter2 <- counter
counter2(); counter(); counter()
identical(counter, counter2)

# Safe copy
counter2 <- copy(counter)
counter2(); counter(); counter()
identical(counter, counter2)

michaelquinn32/generators documentation built on May 22, 2019, 9:52 p.m.