Search a sequence for its minimum and stop as soon as the lowest possible value is found
jussi.piitulainen at helsinki.fi
Sat Jan 7 03:12:38 EST 2017
Rustom Mody writes:
> On Saturday, Jussi Piitulainen wrote:
>> Paul Rubin writes:
>> > Peter Otten writes:
>> >> How would you implement stopmin()?
>> > Use itertools.takewhile
>> How? It consumes the crucial stop element:
>> it = iter('what?')
>> list(takewhile(str.isalpha, it)) # ==> ['w', 'h', 'a', 't']
>> next(it, 42) # ==> 42
> I was also wondering how…
> In a lazy language (eg haskell) with non-strict foldr (reduce but
> rightwards) supplied non-strict operator this is trivial.
> ie in python idiom with reduce being right_reduce
> reduce(operator.mul, [1,2,0,4,...], 1)
> the reduction would stop at the 0
> Not sure how to simulate this in a strict language like python
> Making fold(r) non-strict by using generators is ok
> How to pass a non-strict operator?
I think it would have to be some really awkward pseudo-operator that
throws an exception when it encounters its zero, and then reduce (or
something outside reduce) would catch that exception. Something like
that could be done but it would still be awkward. Don't wanna :)
You switched to a simpler operator. Would Haskell notice that
def minabs(x, y): return min(x, y, key = abs)
has a meaningful zero? Surely it has its limits somewhere and then the
programmer needs to supply the information.
More information about the Python-list