functional programming with map()

Emile van Sebille emile at fenx.com
Sun Feb 24 23:43:34 EST 2002


"Paul Rubin" 
> Both of those build up a new list of the results, instead of
> discarding the values.  If the f function takes an integer and
> computes a 20-megabyte structure, you've got a problem.  

If you know it's coming, throw it away first:

[x.f() and None for x in items]


> This really
> calls for a generator comprehension as discussed a few weeks back.
> 
>    reduce(lambda x,y:(y.f(),0)[1], items, 0)
> 
> almost works, but invokes f an extra time at the end.
> 
> Someone see how to fix it?

Expanding the above to:

filter(None,[x.f() and None for x in items])

returns an empty list vs 0 for the reduce.  


-- 

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list