limit: Set the limit of an iterator

Description Usage Arguments Details Examples

View source: R/limit.R

Description

The limit of the generator refers to the output series, not the underlying state. This approach tends to be more intuitive, especially when the state is a vector (like the Fibonacci numbers).

Usage

1
limit(.generator, .limit)

Arguments

.generator

A function with the class generator

.limit

A predicate function, returning TRUE or FALSE

Details

When the limiting function returns TRUE, a generator is considered exhausted. Additional calls to an exhausted generator return an error.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# A typical case
counter <- generator(0, function (state) state + 1)
limited <- counter %>% limit(~ state < 4)
limited %>% take(3)

# Returns an error
## Not run: limited()

# Fibonacci numbers
fib <- generator(c(0, 1),
                 ~ c(state[2], sum(state)),
                 ~ state[1])
limited <- fib %>% limit(~ state < 5)
limited %>% take(4)

# Returns an error
## Not run: limited()

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