Skip to main content

while_loop

Function while_loop 

Source
pub fn while_loop<B: Backend, C: Tree<B>>(
    cond_fn: impl Fn(&C) -> Bool<B>,
    body_fn: impl Fn(C) -> C,
    init: C,
) -> C
Expand description

Repeats body_fn while cond_fn holds (lax.while_loop).

ยงExamples

use ruxla_core::{Backend, I32, Native, lax};

fn collatz_steps<B: Backend>(n: I32<B>) -> I32<B> {
    let (_, steps) = lax::while_loop(
        |(n, _): &(I32<B>, I32<B>)| n.gt(1),
        |(n, s)| (lax::select((n % 2).eq(0), n / 2, n * 3 + 1), s + 1),
        (n, I32::new(0)),
    );
    steps
}
assert_eq!(collatz_steps::<Native>(I32::new(27)).value(), 111);