]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/util/iteration/sequence.py
add copy of the GPL
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / util / iteration / sequence.py
1 from .peekable import Peekable
2
3
4 def sequence(*iterables, by=None, compare=None):
5     if compare is not None:
6         compare = compare
7     elif by is not None:
8         compare = lambda i1, i2: by(i1) <= by(i2)
9     else:
10         compare = lambda i1, i2: i1 <= i2
11
12     iterables = [Peekable(it) for it in iterables]
13
14     done = False
15     while not done:
16
17         next_i = None
18
19         for i, it in enumerate(iterables):
20             if not it.empty():
21                 if next_i is None or compare(it.peek(), iterables[next_i].peek()):
22                     next_i = i
23
24         if next_i is None:
25             done = True
26         else:
27             yield next(iterables[next_i])

Community Data Science Collective || Want to submit a patch?