Search a sequence for its minimum and stop as soon as the lowest possible value is found

Rustom Mody rustompmody at gmail.com
Sat Jan 7 02:10:11 EST 2017


On Saturday, January 7, 2017 at 12:26:04 PM UTC+5:30, 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?


More information about the Python-list mailing list