// f: func -> A (stream ends when f returns null)
static IterableIterator iteratorFromFunction(fO f) {
class IFF extends IterableIterator {
A a;
bool done;
public bool hasNext() {
getNext();
ret !done;
}
public A next() {
getNext();
if (done) fail();
A _a = a;
a = null;
ret _a;
}
void getNext() {
if (done || a != null) ret;
a = (A) callF(f);
done = a == null;
}
};
ret new IFF;
}
// optimized version for F0 argument
ifclass F0
static IterableIterator iteratorFromFunction(F0 f) {
ret iteratorFromFunction_f0(f);
}
endif
static IterableIterator iteratorFromFunction(IF0 f) {
ret iteratorFromFunction_if0(f);
}