foldn: Fold a generator N times with a binary function

Description Usage Arguments Examples

View source: R/fold.R

Description

foldn is the generator-equivalent of the function Reduce. It takes a generator, produces .n values and applies a binary function iteratively, returning a scalar.

Usage

1
foldn(.generator, .n, .FUN, ..., .start = 0)

Arguments

.generator

A function with the class generator

.n

The number of iterations to produce

.FUN

A binary fold function to reduce the generator to a single value

...

Additional arguments to .FUN

.start

The starting value for the reduction

Examples

1
2
3
# Sum from 1 to 10
counter <- generator(0, function (state) state + 1)
counter %>% copy %>% foldn(10, `+`)

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