extended list comprehensions
Oliver Steele
steele at cs.brandeis.edu
Sat Jun 1 06:36:01 EDT 2002
fxj at hotmail.com (F. Jamitzky) wrote in message news:<f32195af.0205261316.2482d4ac at posting.google.com>...
> It would be great to have something like a list comprehension for the
> reduce function. It would work in the same way as the comprehension
> for the map function, which is:
>
> [foo(x) for x in xs] <-is the same as-> map(foo,xs)
>
> and maybe it could be written as:
>
> {y=y+x for x in xs} <-would be-> reduce(operator.add,xs)
>
> what do the experts think about that ?
>
> ferdinand
What about using ellipsis? These could do foldl (reduce):
...+x for x in xs
operator.add(..., x) for x in xs
This leaves an obvious extension for foldr (reduce from the right):
swap the identifier and the ellipsis:
x+... for x in xs
operator.add(x, ...) for x in xs
The bug in this (putting aside the question of whether it's worth
adding anything to the language) is that it suggests that ... is a way
to write curry (similar to (x+) and (+x) in Haskell), and then:
[...+x for x in xs]
becomes ambiguous between a singleton list containing a reduction, and
a list of lambdas. Any way to save this?
More information about the Python-list
mailing list