yield_while {peruse}R Documentation

yield_while

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

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

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)


[Package peruse version 0.3.1 Index]