]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/mw/util/iteration/aggregate.py
add copy of the GPL
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / mw / util / iteration / aggregate.py
1 from .peekable import Peekable
2
3
4 def group(it, by=lambda i: i):
5     return aggregate(it, by)
6
7
8 def aggregate(it, by=lambda i: i):
9     it = Peekable(it)
10
11     def chunk(it, by):
12         identifier = by(it.peek())
13         while not it.empty():
14             if identifier == by(it.peek()):
15                 yield next(it)
16             else:
17                 break
18
19     while not it.empty():
20         yield (by(it.peek()), chunk(it, by))

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