yield_while: yield_while

Description Usage Arguments Examples

View source: R/yield_while.R

Description

Keep yielding the next element of an Iterator while a condition is met. A condition is a logical expression involving variables in iter$initial or variables that are defined in the enclosure. Refer to the number of the current iteration with .iter.

Usage

1
yield_while(iter, cond)

Arguments

iter

An Iterator object

cond

A logical expression involving some variable(s) in iter$initial or in the enclosure, so that yield_next() continues being called while the expression returns TRUE

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
collatz <- Iterator({
            if (n %% 2 == 0) n <- n / 2 else n <- n*3 + 1
           },
           initial = list(n = 50),
           yield = n)
yield_while(collatz, n != 1L)

p_success <- 0.5
threshold <- 100
seeds <- 1000:1e6
iter <- Iterator({
        set.seed(seeds[.iter])
        n <- n + sample(c(1,-1), 1, prob = c(p_success, 1 - p_success))
       },
       list(n = 0),
       n)
sequence <- yield_while(iter, n <= threshold)

jacgoldsm/itertools documentation built on March 14, 2021, 8:39 a.m.