from functools import partial
from collections import defaultdict
from itertools import imap

""""
If zero length array is passed, returns ["."].
Otherwise returns the origional array.
Useful for dealing with lists of files, or not.
"""
def empty2dot(paths):
  if len(paths) == 0:
    return ["."]
  else:
    return paths

def compose(f, g):
  def composition(*args, **kwargs):
      return f(g(*args, **kwargs))
  return composition

def concat(l):
  for sublist in l:
    for item in sublist:
      yield item

def concatMap(func):
  return compose(concat, partial(imap, func))

def fmap(func):
  def mapped(collection):
    return imap(func, collection)
  return mapped

def identity(x):
    return x

def groupby(func, l):
  groups = defaultdict(list)
  for i in l:
    groups[func(i)].append(i)
  return groups.items()

def take(count):
  def taker(collection):
    remaining = count
    i = iter(collection)
    while remaining > 0:
      yield i.next()
      remaining = remaining - 1
  return taker

def uniq(l):
  seen = set()
  for i in l:
    if i in seen:
      continue
    else:
      seen.add(i)
      yield i

def irange(start, increment):
  while True :
    yield start
    start += increment

def invert(v):
    return not(v)

def count(iterator):
    c = 0
    for v in iterator:
        c+=1
    return c

def uncurry(func):
  """Wraps func so that the first arg is expanded into list args."""
  def uncurried(list_args, **kwargs):
    return func(*list_args, **kwargs)
  return uncurried

def curry(func):
  """"Wraps func so that a series of args are turned into a single arg list."""
  def curried(*args, **kwargs):
    return func(args, **kwargs)
  return curried

def identify(func):
  """Wrap func so that it returns what comes in."""
  def identified(arg):
    func(arg)
    return arg
  return identified

def transduce(*funcs):
  if funcs:
    foo = funcs[0]
    rest = funcs[1:]
    if rest:
      next_hop = transduce(*rest)
      def transducer(*args, **kwargs):
        return next_hop(foo(*args, **kwargs))
      return transducer
    else: # no rest, foo is final function.
      return foo
  else: # no funcs at all.
    return fmap(identity)

@@ -429,16 +429,110 @@
 sition%0A%0A
+def composeFunctor(f,g):%0A    def composition(x):%0A      return f(g(x))%0A    return composition%0A%0A
 def conc
@@ -1096,16 +1096,74 @@
  taker%0A%0A
+def consume(collection):%0A  for _ in collection:%0A    pass%0A%0A
 def uniq

Add new util functions.