Just for Fun: Accumulate

Some years ago, I came across a neat little shell utility that Mark Dominus had written called accumulate. You can read more about it at his blog.

He thought it so trivial that he worried about insulting the intelligence of his readers by sharing the source code; since I have no readers, I have no such concerns. Here is my Python version:

#!/usr/bin/env python

import sys
"""
An ugly not-quite-port of Mark J. Dominus' 'accumulate' utility,
described at [http://blog.plover.com/prog/accumulate.html]
"""

seen = {}

for line in sys.stdin.readlines():
  line.strip()
  array = line.split()
  key = array[0]
  value = array[1:]
  if key in seen:
    seen[key].extend(value)
  else:
    seen[key] = value

for k, v in seen.iteritems():
  v.reverse()
  print k, ' '.join(v)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s