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)
iter | An |
---|---|
cond | A logical expression involving some variable(s) in |
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