Skip to content

Instantly share code, notes, and snippets.

Created September 3, 2010 13:22
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save anonymous/563872 to your computer and use it in GitHub Desktop.
Save anonymous/563872 to your computer and use it in GitHub Desktop.
def iter_group(queue):
buf = []
prev_key = None
for val in queue:
cur_key, cur_val = val
#print cur_key, cur_val
if cur_key == prev_key or prev_key is None:
buf.append(cur_val)
else:
yield prev_key, buf
buf = []
buf.append(cur_val)
prev_key = cur_key
if buf:
yield cur_key, buf
class MapReduce:
def __init__(self):
self.queue = []
def send(self, a,b):
self.queue.append((a,b))
def count(self):
return len(self.queue)
def __iter__(self):
return iter_group(sorted(self.queue))
x = MapReduce()
for word in "foo bar bar".split():
x.send(word, 1)
for word, ones in x:
print word, sum(ones)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment