Last version using more from the outer template

#!/usr/bin/env rdmd
import std.stdio;
import std.algorithm;
import std.typecons;
import std.array;
import std.range;
import std.traits;

auto byMinimum(Ranges)(Ranges ranges)
{
    auto getNonEmpty()
    {
        return ranges.filter!("!a.empty");
    }

    auto nonEmpty = getNonEmpty;

    auto minimumOfRanges()
    {
        // dfmt off
        return nonEmpty
            .map!(range => tuple!("range", "line")(range, range.front))
            .minElement!("a.line");
        // dfmt on
    }

    ReturnType!(minimumOfRanges) minRangeAndLine;
    struct ByMinimum
    {
        bool empty()
        {
            return nonEmpty.empty;
        }

        auto front()
        {
            minRangeAndLine = minimumOfRanges;
            return minRangeAndLine;
        }

        void popFront()
        {
            nonEmpty = getNonEmpty;
            minRangeAndLine.range.popFront;
        }
    }

    return ByMinimum();
}

void main(string[] files)
{
foreach (n; files[1 .. $].map!(name => File(name).byLine(No.keepTerminator)).array.byMinimum)
    {
        writeln(n.line);
    }
}

Reply via email to