map
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon Aug 31 05:05:17 EDT 2009
En Mon, 31 Aug 2009 05:43:07 -0300, Hendrik van Rooyen
<hendrik at microcorp.co.za> escribió:
> On Monday 31 August 2009 06:55:52 elsa wrote:
>
>> (Ultimately, I want to call myFunc(myList[0], 'booHoo'), myFunc(myList
>> [1], 'booHoo'), myFunc(myList[2], 'booHoo') etc. However, I might want
>> to call myFunc(myList[0], 'woo'), myFunc(myList[1], 'woo'), myFunc
>> (myList[2], 'woo') some other time).
>
> Here is some heretical advice:
>
> Do not use stuff like map and reduce unless they fit what you want to do
> perfectly, and "JustWorks" the first time.
I think of that advice as "orthodox", not "heretical"! (functional guys
are minority here...)
> You have a very clear idea of what you want to do, so why do you not just
> simply write something to do it?
>
> something like this (untested):
>
> def woofer(thefunc,thelist,thething):
> theanswers = []
> for x in thelist:
> theanswers.append(thefunc(x,thething))
> return theanswers
>
> And the advantage is that you do not have to remember what map does...
This block:
theanswers = []
for x in thelist:
theanswers.append(thefunc(x,thething))
is formally the same as this one:
theanswers = [thefunc(x,thething) for x in thelist]
but the list comprehension is faster. So the function becomes:
def woofer(thefunc,thelist,thething):
return [thefunc(x,thething) for x in thelist]
and may be inlined (it's usually easier to read).
> *ducks away from the inevitable flames*
*fights back to back with you against heretics*
--
Gabriel Genellina
More information about the Python-list
mailing list