[Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence
Peter Otten
__peter__ at web.de
Mon Oct 25 19:10:56 CEST 2010
Guido van Rossum wrote:
> On Sun, Oct 24, 2010 at 2:59 PM, Lie Ryan
> <lie.1296 at gmail.com> wrote:
>> There is a way, by using threading, and injecting a thread-safe tee into
>> max/min/otherFuncs (over half of the code is just for implementing
>> thread-safe tee). Using this, there is no need to make any modification
>> to min/max. I suppose it might be possible to convert this to using the
>> new coroutine proposal (though I haven't been following the proposal
>> close enough).
>>
>> The code is quite slow (and ugly), but it can handle large generators
>> (or infinite generators). The memory shouldn't grow if the functions in
>> *funcs takes more or less similar amount of time (which is true in case
>> of max and min); if *funcs need to take both very fast and very slow
>> codes at the same time, some more code can be added for load-balancing
>> by stalling faster threads' request for more items, until the slower
>> threads finishes.
>>
>> Pros:
>> - no modification to max/min
>>
>> Cons:
>> - slow, since itertools.tee() is reimplemented in pure-python
>> - thread is uninterruptible
> [snip]
>
> This should not require threads.
>
> Here's a bare-bones sketch using generators:
> outcome = func(outcome, val)
I don't think the generator-based approach is equivalent to what Lie Ryan's
threaded code does. You are calling max(a, b) 99 times while Lie calls
max(items) once.
Is it possible to calculate min(items) and max(items) simultaneously using
generators? I don't see how...
Peter
More information about the Python-ideas
mailing list