Replace reduce with listcomprehension?

S

ssecorp

GvR wants to eliminate all the functional stuff (lambda map reduce
filter) which kind of makes sense for Python, listcomprehensions are
more efficient(at least as implemented inpython) from what i have
gathered and they can express everything map/reduce/filter with
crippled lambdas can.

but what about this:
reduce(lambda x,y:x*y+x,[1,2,3,4,5,6])

how would I express that in a listcomprehension?
 
D

Diez B. Roggisch

ssecorp said:
GvR wants to eliminate all the functional stuff (lambda map reduce
filter) which kind of makes sense for Python, listcomprehensions are
more efficient(at least as implemented inpython) from what i have
gathered and they can express everything map/reduce/filter with
crippled lambdas can.

but what about this:
reduce(lambda x,y:x*y+x,[1,2,3,4,5,6])

how would I express that in a listcomprehension?

You can't. And AFAIK your are wrong - GvR doesn't wan to eliminate the
functional stuff.

http://www.artima.com/weblogs/viewpost.jsp?thread=98196
http://www.python.org/dev/peps/pep-3099/



Diez
 
L

Luis M. González

Correction:
I guess the link I provided above is old news...
In the more recent Python 3000 FAQ, GvR says:

Q. If you're killing reduce(), why are you keeping map() and filter()?

A. I'm not killing reduce() because I hate functional programming; I'm
killing it because almost all code using reduce() is less readable
than the same thing written out using a for loop and an accumulator
variable. On the other hand, map() and filter() are often useful and
when used with a pre-existing function (e.g. a built-in) they are
clearer than a list comprehension or generator expression. (Don't use
these with a lambda though; then a list comprehension is clearer and
faster.)

So it seems only reduce will be eliminated.

Luis
 
D

Diez B. Roggisch

Luis said:
Correction:
I guess the link I provided above is old news...
In the more recent Python 3000 FAQ, GvR says:

Q. If you're killing reduce(), why are you keeping map() and filter()?

A. I'm not killing reduce() because I hate functional programming; I'm
killing it because almost all code using reduce() is less readable
than the same thing written out using a for loop and an accumulator
variable. On the other hand, map() and filter() are often useful and
when used with a pre-existing function (e.g. a built-in) they are
clearer than a list comprehension or generator expression. (Don't use
these with a lambda though; then a list comprehension is clearer and
faster.)

So it seems only reduce will be eliminated.

Nope. From the link you provided yourself:

"""
Only reduce will be removed from the 3.0 standard library. You can
import it from functools.
"""

Diez
 
B

bearophileHUGS

Luis M. González, citing Guido:
A. I'm not killing reduce() because I hate functional programming; I'm
killing it because almost all code using reduce() is less readable
than the same thing written out using a for loop and an accumulator
variable.

(So reduce is now in itertools). Try to pry folds from the hands of
haskell programmers :)
I think they think that folds are more clear than explicit loops...

Bye,
bearophile
 
S

Steven D'Aprano

Nope. From the link you provided yourself:

"""
Only reduce will be removed from the 3.0 standard library. You can
import it from functools.
"""

functools isn't in the standard library???

*wink*

Seriously, I think Guido meant "built-ins". Since I don't agree with him
that reduce() is hard to read, I disagree that accumulators are easier to
use. But since reduce is only an import away, I'm satisfied.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top