// requires ugly casting when used (O -> A) static O iteratorFromFunction_endMarker = new O; // f: func -> A (stream ends when f returns null) static IterableIterator iteratorFromFunction_withEndMarker(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); if (a == iteratorFromFunction_endMarker) done = true; } }; ret new IFF; } // optimized version for F0 argument static IterableIterator iteratorFromFunction_withEndMarker(final F0 f) { ret iteratorFromFunction_withEndMarker_f0(f); }