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

35
LINES

< > BotCompany Repo | #1022233 // repeatFunctionReturningList_iterator

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

Libraryless. Click here for Pure Java version (1770L/12K).

1  
// f : A -> Collection<A>
2  
static <A> ItIt<A> mapLike repeatFunctionReturningList_iterator(O f, A input, O... _) {
3  
  final Set<A> seen = optParOr seen(_, () -> (Set) new HashSet);
4  
  final Int max = cast optPar max(_);
5  
  bool plusInput = boolPar plusInput(_); // return original input too?
6  
  
7  
  // Make pool of iterators
8  
  final new LinkedList<Iterator<A>> pool;
9  
  if (plusInput) pool.add(singletonIterator(input));
10  
  else seen.add(input);
11  
  pool.add(iterator((Iterable<A>) callF(f, input)));
12  
  ret iteratorFromFunction(new F0<A> {
13  
    int n = 0;
14  
    
15  
    public A get() {
16  
      if (max != null && n >= max) null;
17  
      while ping (!empty(pool)) {
18  
        Iterator<A> it = first(pool);
19  
        if (!it.hasNext()) continue with removeFirst(pool);
20  
        
21  
        // Get entry and check if seen already
22  
        A entry = it.next();
23  
        if (!seen.add(entry)) continue;
24  
  
25  
        // found new entry - return and schedule for further analysis
26  
        Collection<A> newStuff = cast callF(f, entry);
27  
        if (nempty(newStuff))
28  
          pool.add(iterator(newStuff));
29  
        ++n;
30  
        ret entry;
31  
      }
32  
      null;
33  
    }
34  
  });
35  
}

Author comment

Began life as a copy of #1018118

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1022233
Snippet name: repeatFunctionReturningList_iterator
Eternal ID of this version: #1022233/10
Text MD5: f231a8b8fd9c4887ddf173696dfc297f
Transpilation MD5: 8c18ed3847525fa2c8fd43c9499bdde5
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-06-26 18:25:58
Source code size: 1166 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 236 / 326
Version history: 9 change(s)
Referenced in: [show references]