
The alternative I came up with was:
y = (from result = 0.0 do result += x**2 for x in values if x > 0)
As has been pointed out, this hardly gains you anything over writing it all out explicitly. It seems like nothing more than a Perlesque another-way-to-do-it. This seems to be the fate of all reduce-replacement suggestions that try to be fully general -- there are just too many degrees of freedom to be able to express it all succinctly. The only way out of this I can see (short of dropping the whole idea) is to cut out some of the degrees of freedom by restrict ourselves to targeting the most common cases. Thinking about the way this works in APL, where you can say things like total = + / numbers one reason it's so compact is that the system knows what the identity is for each operator, so you don't have to specify the starting value explicitly. Another is the use of a binary operator. So if we postulate a "reducing protocol" that requires function objects to have a __div__ method that performs reduction with a suitable identity, then we can write total = operator.add / numbers Does that look succinct enough? Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+