Hi 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? cheers,
On Mon, Oct 31, 2005, Martin Blais wrote:
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?
This should go on comp.lang.python. Thanks. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair
On 10/31/05, Martin Blais <blais@furius.ca> wrote:
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) [...] Just wondering, does anybody know how to do this nicely? Is there an easy form that allows me to do this?
FWIW, something like this works:
def fpow(f, n): ... def res(*args, **kw): ... nn = n ... while nn > 0: ... args = [f(*args, **kw)] ... kw = {} ... nn -= 1 ... return args[0] ... return res ...
fn = r'a\b\c\d\e\f\g' d3 = fpow(os.path.dirname, 3) d3(fn) 'a\\b\\c\\d'
You can vary this a bit - the handling of keyword arguments is an obvious place where I've picked a very arbitrary approach - but you get the idea. This *may* be a candidate for addition to the new "functional" module, but I'd be surprised if it got added without proving itself "in the wild" first. More likely, it should go in a local "utilities" module. Paul.
Martin Blais wrote:
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)
Maybe ** should be defined for functions so that you could do things like up3levels = dirname ** 3 -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg.ewing@canterbury.ac.nz +--------------------------------------+
I'm always--literally every time-- looking for a more functional
[Martin Blais] form,
something that would be like this:
# apply dirname() 3 times on its results, initializing with p ... = repapply(dirname, 3, p)
[Greg Ewing]
Maybe ** should be defined for functions so that you could do things like
up3levels = dirname ** 3
Hmm, using the function's own namespace is an interesting idea. It might also be a good place to put other functionals: results = f.map(data) newf = f.partial(somearg) Raymond
Raymond Hettinger wrote:
I'm always--literally every time-- looking for a more functional
[Martin Blais] form,
something that would be like this:
# apply dirname() 3 times on its results, initializing with p ... = repapply(dirname, 3, p)
[Greg Ewing]
Maybe ** should be defined for functions so that you could do things like
up3levels = dirname ** 3
Hmm, using the function's own namespace is an interesting idea. It might also be a good place to put other functionals:
results = f.map(data) newf = f.partial(somearg)
And we have solved the "map, filter and reduce are going away! Let's all weep together" problem with one strike! Reinhold -- Mail address is perfectly valid!
On 11/1/05, Reinhold Birkenfeld <reinhold-birkenfeld-nospam@wolke7.net> wrote:
Hmm, using the function's own namespace is an interesting idea. It might also be a good place to put other functionals:
results = f.map(data) newf = f.partial(somearg)
And we have solved the "map, filter and reduce are going away! Let's all weep together" problem with one strike!
Reinhold
I have no problems with map and filter goint away. About reduce - please remember that you need to add this method to any callable, including every type (I mean the constructor). I am not sure it is a good trade for throwing away one builting, which is a perfectly reasonable function. Noam
Reinhold Birkenfeld wrote:
Raymond Hettinger wrote:
[Martin Blais]
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)
[Greg Ewing]
Maybe ** should be defined for functions so that you could do things like
up3levels = dirname ** 3
Hmm, using the function's own namespace is an interesting idea. It might also be a good place to put other functionals:
results = f.map(data) newf = f.partial(somearg)
And we have solved the "map, filter and reduce are going away! Let's all weep together" problem with one strike!
not really, those right now work with any callable,
class C: ... def __call__(self, x): ... return 2*x ... map(C(), [1,2,3]) [2, 4, 6]
that's why attaching functionaliy as methods is not always the best solution. regards.
[Greg Ewing]
Maybe ** should be defined for functions so that you could do things like
up3levels = dirname ** 3
[Raymond Hettinger]
Hmm, using the function's own namespace is an interesting idea. It might also be a good place to put other functionals:
results = f.map(data) newf = f.partial(somearg)
Sorry to rain on everybody's parade, but I don't think so. There are many different types of callables. This stuff would only work if they all implemented the same API. That's unlikely to happen. A module with functions to implement the various functional operations has much more potential. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 1 nov 2005, at 22.40, Guido van Rossum wrote:
[Greg Ewing]
Maybe ** should be defined for functions so that you could do things like
up3levels = dirname ** 3
[Raymond Hettinger]
Hmm, using the function's own namespace is an interesting idea. It might also be a good place to put other functionals:
results = f.map(data) newf = f.partial(somearg)
Sorry to rain on everybody's parade, but I don't think so. There are many different types of callables. This stuff would only work if they all implemented the same API. That's unlikely to happen. A module with functions to implement the various functional operations has much more potential.
Perhaps then a decorator that uses these functions? //Simon
participants (10)
-
Aahz -
Greg Ewing -
Guido van Rossum -
Martin Blais -
Noam Raphael -
Paul Moore -
Raymond Hettinger -
Reinhold Birkenfeld -
Samuele Pedroni -
Simon Percivall