This example starts a collection with the pairs (x, x) for x in 0 .. 10. It then
repeatedly adds any pairs (x, z) for (x, y) in the collection, where z is the Collatz
step for y: it is y/2 if y is even, and 3*y + 1 if y is odd. This produces all of the
pairs (x, y) where x visits y as part of its Collatz journey.
use datafrog::{Iteration, Relation};
let mut iteration = Iteration::new();
let variable = iteration.variable::<(usize, usize)>("source");
variable.insert(Relation::from((0 .. 10).map(|x| (x, x))));
while iteration.changed() {
variable.from_map(&variable, |&(key, val)|
if val % 2 == 0 {
(key, val/2)
}
else {
(key, 3*val + 1)
});
}
let result = variable.complete();
assert_eq!(result.len(), 74);
Adds tuples that result from mapping input.
Examples
This example starts a collection with the pairs (x, x) for x in 0 .. 10. It then repeatedly adds any pairs (x, z) for (x, y) in the collection, where z is the Collatz step for y: it is y/2 if y is even, and 3*y + 1 if y is odd. This produces all of the pairs (x, y) where x visits y as part of its Collatz journey.