Steven said:
Don't disagree with that.
Same goes for sum. Same goes for product, ...
Each item needs to stand on it's own. It's a much stronger argument for
removing something because something else fulfills it's need and is
easier or faster to use than just saying we need x because we have y.
In this case sum and product fulfill 90% (estimate of course) of reduces
use cases. It may actually be as high as 99% for all I know. Or it may
be less. Anyone care to try and put a real measurement on it?
which doesn't have that many
common usages apart from calculating the geometric mean, and let's face
it, most developers don't even know what the geometric mean _is_.
I'm neutral on adding product myself.
If you look back at past discussions about sum, you will see that there is
plenty of disagreement about how it should work when given non-numeric
arguments, eg strings, lists, etc. So it isn't so clear what sum should do.
Testing shows sum() to be over twice as fast as either using reduce or a
for-loop. I think the disagreements will be sorted out.
That is an optimization issue. Especially when used with the operator
module, reduce and map can be significantly faster than for loops.
I tried it... it made about a 1% improvement in the builtin reduce and
an equal improvement in the function that used the for loop.
The inline for loop also performed about the same.
See below..
Disagree about product, although given that sum is in the language, it
doesn't hurt to put product as well for completion and those few usages.
I'm not convinced about product either, but if I were to review my
statistics textbooks, I could probably find more uses for it. I suspect
that there may be a few common uses for it that are frequent enough to
make it worth adding. But it might be better in a module.
I don't object to adding sum and product to the language. I don't object
to adding zip. I don't object to list comps. Functional, er, functions
are a good thing. We should have more of them, not less.
Yes, we should have lots of functions to use, in the library, but not
necessarily in builtins.
Another slippery slope argument.
Do you disagree or agree? Or are you undecided?
Because it is already there.
Hmm.. I know a few folks, Good people, but they keep everything to the
point of not being able to find anything because they have so much.
They can always think of reasons to keep things, "It's worth something",
"it means something to me", "I'm going to fix it", "I'm going to sell
it", "I might need it". etc..
"Because it is already there" sound like one of those type of reasons.
Yes, let's all re-invent the wheel in every module! Why bother having a
print statement, when it is so easy to write your own:
def myprint(obj):
sys.stdout.write(str(obj))
Yes, Guido wants to make print a function in Python 3000. The good
thing about this is you can call your function just 'p' and save some
typing.
p("hello world")
Actually, I think i/o functions should be grouped in an interface
module. That way you choose the interface that best fits your need. It
may have a print if it's a console, or it may have a widget if it's a gui.
Best of all, you can customize print to do anything you like, _and_ it is
a function.
Because that is far less readable, and you take a performance hit.
They come out pretty close as far as I can tell.
def reduce_f( f, seq):
x = seq[0]
for y in seq[1:]:
x = f(x,y)
return x
import time
t = time.time()
r2 = reduce(lambda x,y: x*y, range(1,10000))
t2 = time.time()-t
print 'reduce builtin:', t2
t = time.time()
r1 = reduce_f(lambda x,y: x*y, range(1,10000))
t2 = time.time()-t
print 'reduce_f: ', t2
if r1!=r2: print "results not equal"
reduce builtin: 0.156000137329
reduce_f: 0.155999898911reduce builtin: 0.15700006485
reduce_f: 0.155999898911reduce builtin: 0.141000032425
reduce_f: 0.155999898911
That's your choice. I'm not suggesting we remove for loops and force you
to use reduce. Or even list comps.
Just don't force me to use decorators! ;-)
Nah, they're ok too, but it did take me a little while to understand
their finer points.
Cheers,
Ron