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.

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)
#> [1] 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
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)