map/filter/reduce/lambda opinions and background unscientific mini-survey

Christopher Subich spam.csubich+block at block.subich.spam.com
Sun Jul 3 07:01:18 CEST 2005


Steven D'Aprano wrote:
> comps. But reduce can't be written as a list comp, only as a relatively
> complex for loop at a HUGE loss of readability -- and I've never used
> Lisp or Scheme in my life. I'm surely not the only one.

See my reply to your other post for a more detailed explanation, but I 
don't think that the for-loop solution is much less readable at all, and 
the additional complexity involved is simply setting the initial value 
and result for the accumulator.  The for-loop solution is even more 
flexible, because it can include anonymous code blocks and not just 
expressions.

One caevat that I just noticed, though -- with the for-solution, you do 
need to be careful about whether you're using a generator or list if you 
do not set an explicit initial value (and instead use the first value of 
'sequence' as the start).  The difference is:
_accum = g.next()
for i in g: _accum = stuff(_accum,i)

versus
_accum = g[0]
for i in g[1:]: _accum = stuff(_accum,i)

The difference is because generators don't support subscripts, while 
lists don't support .next() iteration.  Unless I'm missing something in 
the language (entirely possible), this suggests a missing feature for 
same-syntax iteration over the two types.



More information about the Python-list mailing list