// requires ugly casting when used (O -> A)
static O iteratorFromFunction_endMarker = new O;

// f: func -> A | endMarker
static <A> IterableIterator<A> iteratorFromFunction_withEndMarker(fO f) {
  class IFF extends IterableIterator<A> {
    A a;
    bool have, done;
    
    public bool hasNext() {
      getNext();
      ret !done;
    }
    
    public A next() {
      getNext();
      if (done) fail();
      A _a = a;
      a = null;
      have = false;
      ret _a;
    }
    
    void getNext() {
      if (done || have) ret;
      O o = callF(f);
      if (o == iteratorFromFunction_endMarker)
        ret with done = true;
      a = (A) o;
      set have;
    }
  };
  ret new IFF;
}

// optimized version for F0 argument; TODO: do same for IF0
static <A> IterableIterator<A> iteratorFromFunction_withEndMarker(final F0 f) {
  ret iteratorFromFunction_withEndMarker_f0(f);
}