If I have a range like this, how do you find its last item using Phobos?

A simple way is to use seq.array.back, but this wastes memory. Another simple way is to use a foreach loop keeping the last seen. But do you know if there is a function to do in Phobos? And if such function is not present (walkBack? backWalk?) is it a good idea to add it to Phobos?


void main() {
    import std.stdio, std.algorithm, std.range;

    int m = 75;
    auto seq = recurrence!q{ a[n - 1] + a[n - 2] }(1, 1)
               .until!(x => x > m)(OpenRight.no);

    seq.array.back.writeln; // 89
    //seq.back.writeln;

    auto last = -1;
    foreach (immutable x; seq)
        last = x;
    last.writeln;
}


Bye,
bearophile

Reply via email to