[Python-Dev] a different kind of reduce...

Martin Blais blais at furius.ca
Mon Oct 31 20:13:05 CET 2005


I find myself occasionally doing this:

   ... = dirname(dirname(dirname(p)))

I'm always--literally every time-- looking for a more functional form,
something that would be like this:

   # apply dirname() 3 times on its results, initializing with p
   ... = repapply(dirname, 3, p)

There is a way to hack something like that with reduce, but it's not
pretty--it involves creating a temporary list and a lambda function:

  ... = reduce(lambda x, y: dirname(x), [p] + [None] * 3)

Just wondering, does anybody know how to do this nicely? Is there an
easy form that allows me to do this?


More information about the Python-Dev mailing list