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

38
LINES

< > BotCompany Repo | #1006726 // iteratorFromFunction - uses null to mark end of stream

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

Libraryless. Click here for Pure Java version (3074L/17K).

// f: func -> A (stream ends when f returns null)
static <A> IterableIterator<A> iteratorFromFunction(fO f) {
  class IFF extends IterableIterator<A> {
    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 <A> IterableIterator<A> iteratorFromFunction(F0<A> f) {
  ret iteratorFromFunction_f0(f);
}
endif

static <A> IterableIterator<A> iteratorFromFunction(IF0<A> f) {
  ret iteratorFromFunction_if0(f);
}

Author comment

Began life as a copy of #1005525

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, elmgxqgtpvxh, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006726
Snippet name: iteratorFromFunction - uses null to mark end of stream
Eternal ID of this version: #1006726/10
Text MD5: e89edc854bbc8f33b3de846a485709d6
Transpilation MD5: 32cef42f9ee9e52a84d41c3a0f13e169
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-11 15:56:21
Source code size: 797 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 492 / 591
Version history: 9 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1012440 - iteratorFromFunction_f0 - ends on null
#1012670 - iteratorFromFunction_withEndMarker