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

// f : A -> Collection<A>
static <A> ItIt<A> mapLike repeatFunctionReturningList_iterator(O f, A input, O... _) {
  final Set<A> seen = optParOr seen(_, () -> (Set) new HashSet);
  final Int max = cast optPar max(_);
  bool plusInput = boolPar plusInput(_); // return original input too?
  
  // Make pool of iterators
  final new LinkedList<Iterator<A>> pool;
  if (plusInput) pool.add(singletonIterator(input));
  else seen.add(input);
  pool.add(iterator((Iterable<A>) callF(f, input)));
  ret iteratorFromFunction(new F0<A> {
    int n = 0;
    
    public A get() {
      if (max != null && n >= max) null;
      while ping (!empty(pool)) {
        Iterator<A> it = first(pool);
        if (!it.hasNext()) continue with removeFirst(pool);
        
        // Get entry and check if seen already
        A entry = it.next();
        if (!seen.add(entry)) continue;
  
        // found new entry - return and schedule for further analysis
        Collection<A> newStuff = cast callF(f, entry);
        if (nempty(newStuff))
          pool.add(iterator(newStuff));
        ++n;
        ret entry;
      }
      null;
    }
  });
}

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: 355 / 431
Version history: 9 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1022458 - transitiveHullOfFunction
#1023182 - functionReturningList_transitiveHull_ciSet