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

32
LINES

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

JavaX fragment (include)

// 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
static <A> IterableIterator<A> iteratorFromFunction(final F0<A> f) {
  ret iteratorFromFunction_f0(f);
}

Author comment

Began life as a copy of #1005525

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1006726
Snippet name: iteratorFromFunction - uses null to mark end of stream
Eternal ID of this version: #1006726/9
Text MD5: e4ef39ccce201f8bec3fe568b36f0aa9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-02 14:17:03
Source code size: 678 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 292 / 335
Version history: 8 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1006726 & http://1006726.tinybrain.de