Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

39
LINES

< > BotCompany Repo | #1012670 // iteratorFromFunction_withEndMarker

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2223L/14K).

// 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);
}

Author comment

Began life as a copy of #1006726

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1012670
Snippet name: iteratorFromFunction_withEndMarker
Eternal ID of this version: #1012670/10
Text MD5: 079337852ad4d61ae8fa3b4d6eb9313d
Transpilation MD5: 1170bda7f0684a651bc5e269ea15291d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-28 11:47:45
Source code size: 924 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 467 / 1049
Version history: 9 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1013588 - iteratorFF - synonym of iteratorFromFunction_withEndMarker