Adam D. Ruppe:

struct query {
    private Result result;
    this(string q) {
         result = c_query(toStringz(q));
         if(!empty) popFront();
    }

    Row front;
    @property bool empty() { return HasRow(result); }
    void popFront() in { assert(!empty); } body {
         front = GetNextRow(result);
    }
}


It is certainly a bit longer, but it isn't that bad, and is easily extended to other range capabilities.

Your code is badly formatted.


Translating recursive iteration to a range does take a bit more, you need to track your local variables and put them in a stack of your own, but even that isn't too hard (though a bit wordier).

In generally this is rather bad, unless you are a C programmer used to use tweezers and needles to implement your own hash set every other day :-(


I guess the whole yield thing can be kinda nice, I'm just not sure it is that big of a win given its other limitations compared to full ranges.

yield is limited, but in a large amount of cases it's enough, especially if it's well implemented (now Python 3 has yield that is usable for coroutines too, and it has recently added another optimization). For the other cases you can use normal range protocols as you have done.

Bye,
bearophile

Reply via email to