Description Usage Arguments Examples
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
.
1 | yield_while(iter, cond)
|
iter |
An |
cond |
A logical expression involving some variable(s) in |
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.