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).

1  
// f: func -> A (stream ends when f returns null)
2  
static <A> IterableIterator<A> iteratorFromFunction(fO f) {
3  
  class IFF extends IterableIterator<A> {
4  
    A a;
5  
    bool done;
6  
    
7  
    public bool hasNext() {
8  
      getNext();
9  
      ret !done;
10  
    }
11  
    
12  
    public A next() {
13  
      getNext();
14  
      if (done) fail();
15  
      A _a = a;
16  
      a = null;
17  
      ret _a;
18  
    }
19  
    
20  
    void getNext() {
21  
      if (done || a != null) ret;
22  
      a = (A) callF(f);
23  
      done = a == null;
24  
    }
25  
  };
26  
  ret new IFF;
27  
}
28  
29  
// optimized version for F0 argument
30  
ifclass F0
31  
static <A> IterableIterator<A> iteratorFromFunction(F0<A> f) {
32  
  ret iteratorFromFunction_f0(f);
33  
}
34  
endif
35  
36  
static <A> IterableIterator<A> iteratorFromFunction(IF0<A> f) {
37  
  ret iteratorFromFunction_if0(f);
38  
}

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: 499 / 601
Version history: 9 change(s)
Referenced in: [show references]