//! Module defining a right-fold function. rfold = |xs, acc, fn| { (_, acc) = while( (xs, acc), |(xs, _)| xs as [_] != (), |(xs, acc)| { (...head: Num, tail: Num) = xs as any; (head, fn(acc, tail)) }, ); acc }; // Check that `rfold` works with different accumulator types. folded = rfold((1, 2, 3), () as [_], push); assert_eq(folded, (3, 2, 1)); rfold((4, 5, 6), true, |acc, x| acc && x > 0); (min, max) = rfold((1, 2, 3, 4), (INF, -INF), |(min, max), x| { min = if(x < min, x, min); max = if(x > max, x, max); (min, max) }); // Export the `rfold` function. rfold