wrap: Modify a generators wrapping function, spawning a new...

Description Usage Arguments Details Examples

View source: R/wrap.R

Description

Wrapping is the standard technique for spawning new generators. By default all generators are wrapped in the identity function. When applying wrap, we take the value currently returned by the generator and use it as the argument in another function. To be safe, the function spawns a new generator. Updating the second generator does not affect the first.

Usage

1
wrap(.generator, .wrap)

Arguments

.generator

A function with the class generator

.wrap

A function that takes state as an argument

Details

Wrapping is also the engine for a variety of other standard modifications to generators, including filtering.

Examples

1
2
3
4
5
# Create a counter than returns the square values of the natural numbers
counter <- generator(0, function (state) state + 1)
squares <- function(state) state ^ 2
squared <- counter %>% wrap(squares)
squared %>% take(5)

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