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.
import std.algorithm : map, filter; import std.range : iota; import datacat : Iteration, kvTuple; // arrange Iteration iter; auto variable = iter.variable!(int, int)("source"); variable.insert(iota(10).map!(x => kvTuple(x, x))); // act while (iter.changed) { variable.fromMap!((a) => a.value % 2 == 0 ? kvTuple(a.key, a.value / 2) : kvTuple(a.key, 3 * a.value + 1))(variable); } auto result = variable.complete; // assert result.length.should == 74;
Adds tuples that result from mapping input.