std::ranges::generate

cppreference.com

Example

dice: type = {
    distr:  std::uniform_int_distribution<int> = (1, 6);
    device: std::random_device;
    engine: std::mt19937 = (device());

    roll: (inout this) -> int = {
        return distr(engine);
    }
}

iota: (inout v, n: int) = {
    std::ranges::generate(v, :() n$++);
}

main: () = {
    v: std::array<int, 8> = ();

    dice: dice = ();

    std::ranges::generate(v.begin(), v.end(), :() dice&$*.roll());
    std::println("dice: {}", v);
    std::ranges::generate(v, :() dice&$*.roll());
    std::println("dice: {}", v);

    iota(v, 0);
    std::println("iota: {}", v);
}

Possible output

dice: [1, 2, 1, 6, 5, 6, 1, 6]
dice: [6, 5, 5, 1, 2, 4, 2, 1]
iota: [1, 2, 3, 4, 5, 6, 7, 8]

Back to top

cpp2reference.com licensed under CC-BY-SA and GFDL.