Monthly Archives: September 2013

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)