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

2017-01-09 Thread Peter Otten
Antonio Caminero Garcia wrote: > On Friday, January 6, 2017 at 6:04:33 AM UTC-8, Peter Otten wrote: >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the buil

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

2017-01-08 Thread Antonio Caminero Garcia
On Friday, January 6, 2017 at 6:04:33 AM UTC-8, Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The s

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

2017-01-08 Thread Paul Rubin
Jussi Piitulainen writes: > It could still be added as an option, to both takewhile and iter(_, _). That's too messy, it really should be pervasive in iterators. -- https://mail.python.org/mailman/listinfo/python-list

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

2017-01-08 Thread Paul Rubin
Peter Otten <__pete...@web.de> writes: > return min(take_until(), key=firstitem)[1] Actually, key=abs should work. I realized that after posting. -- https://mail.python.org/mailman/listinfo/python-list

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

2017-01-08 Thread Jussi Piitulainen
Paul Rubin writes: > I think Python's version of iterators is actually buggy and at least > the first element of the rest of the sequence should be preserved. > There are ways to fake it but they're too messy for something like > this. It should be the default and might have been a good change fo

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

2017-01-08 Thread Jussi Piitulainen
Paul Rubin writes: > Jussi Piitulainen writes: >> That would return 0 even when there is no 0 in xs at all. > > Doesn't look that way to me: > > >>> minabs([5,3,1,2,4]) > 1 Sorry about that. I honestly meant to say it would return 1 even when there was a single 0 at the very end. Somehow

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

2017-01-08 Thread Peter Otten
Paul Rubin wrote: > Paul Rubin writes: >> seems to work, but is ugly. Maybe there's something better. > > def minabs2(xs): > def z(): > for x in xs: > yield abs(x), x > if x==0: break > return min(z())[1] > > is the same thing but

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

2017-01-08 Thread Paul Rubin
Paul Rubin writes: > seems to work, but is ugly. Maybe there's something better. def minabs2(xs): def z(): for x in xs: yield abs(x), x if x==0: break return min(z())[1] is the same thing but a little bit nicer. -- https://mail.py

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

2017-01-08 Thread Paul Rubin
Paul Rubin writes: > Doesn't look that way to me: > >>> minabs([5,3,1,2,4]) > 1 There's a different problem though: >>> minabs([1,2,3,0]) 1 I think Python's version of iterators is actually buggy and at least the first element of the rest of the sequence should be preserved. Th

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

2017-01-08 Thread Paul Rubin
Jussi Piitulainen writes: > That would return 0 even when there is no 0 in xs at all. Doesn't look that way to me: >>> minabs([5,3,1,2,4]) 1 -- https://mail.python.org/mailman/listinfo/python-list

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

2017-01-07 Thread Jussi Piitulainen
Pablo Lucena writes: > How about using the second usage of builtin iter()? > > In [92]: iter? > Docstring: > iter(iterable) -> iterator > iter(callable, sentinel) -> iterator Nice to learn about that. But it has the same problem as itertools.takewhile: > In [88]: numbers > Out[88]: [1, 9, 8, 11,

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

2017-01-07 Thread Jussi Piitulainen
Paul Rubin writes: > Jussi Piitulainen writes: >>> Use itertools.takewhile >> How? It consumes the crucial stop element: > > Oh yucch, you're right, it takes it from both sides. How about this: > > from itertools import takewhile, islice > def minabs(xs): > a = iter(xs) > m =

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

2017-01-07 Thread Paul Rubin
Jussi Piitulainen writes: >> Use itertools.takewhile > How? It consumes the crucial stop element: Oh yucch, you're right, it takes it from both sides. How about this: from itertools import takewhile, islice def minabs(xs): a = iter(xs) m = min(map(abs,takewhile(lambda x:

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

2017-01-07 Thread Pablo Lucena
How about using the second usage of builtin iter()? In [92]: iter? Docstring: iter(iterable) -> iterator iter(callable, sentinel) -> iterator Get an iterator from an object. In the first form, the argument must supply its own iterator, or be a sequence. *In the second form, the callable is calle

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

2017-01-07 Thread Jussi Piitulainen
Rustom Mody writes: > On a Saturday, Jussi Piitulainen wrote: [snip] >> 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

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

2017-01-07 Thread Rustom Mody
On Saturday, January 7, 2017 at 1:42:47 PM UTC+5:30, Jussi Piitulainen wrote: > Rustom Mody writes: > > > On Saturday, Jussi Piitulainen wrote: > >> Paul Rubin writes: > >> > >> > Peter Otten writes: > >> >> How would you implement stopmin()? > >> > > >> > Use itertools.takewhile > >> > >> How?

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

2017-01-07 Thread Peter Otten
Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the use

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

2017-01-07 Thread Peter Otten
Steve D'Aprano wrote: > On Sat, 7 Jan 2017 01:04 am, Peter Otten wrote: > >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the builtin min() will continue.

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

2017-01-07 Thread Peter Otten
Paul Rubin wrote: > Peter Otten <__pete...@web.de> writes: >> How would you implement stopmin()? > > Use itertools.takewhile I should have mentioned that I had already run into that -- let's call it -- off-by-one bug. -- https://mail.python.org/mailman/listinfo/python-list

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

2017-01-07 Thread Peter Otten
Wolfgang Maier wrote: > On 1/6/2017 15:04, Peter Otten wrote: >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the builtin min() will continue. >> >> The sol

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

2017-01-07 Thread Peter Otten
Jussi Piitulainen wrote: > Peter Otten writes: > >> Example: you are looking for the minimum absolute value in a series of >> integers. As soon as you encounter the first 0 it's unnecessary extra >> work to check the remaining values, but the builtin min() will continue. >> >> The solution is a m

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

2017-01-07 Thread Steve D'Aprano
On Sat, 7 Jan 2017 07:06 am, Wolfgang Maier wrote: > On 1/6/2017 15:04, Peter Otten wrote: [...] >> How would you implement stopmin()? >> > > How about: > > def stopmin (iterable, key, stop): > def take_until (): > for e in iterable: > yield e > if key(e)

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

2017-01-07 Thread Jussi Piitulainen
Chris Angelico writes: > On Sat, Jan 7, 2017 at 7:12 PM, Jussi Piitulainen wrote: >> 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 need

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

2017-01-07 Thread Chris Angelico
On Sat, Jan 7, 2017 at 7:12 PM, Jussi Piitulainen wrote: > 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. If

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

2017-01-07 Thread Jussi Piitulainen
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,

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

2017-01-07 Thread Steve D'Aprano
On Sat, 7 Jan 2017 01:04 am, Peter Otten wrote: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum

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

2017-01-06 Thread Rustom Mody
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.i

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

2017-01-06 Thread Jussi Piitulainen
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 -- https://mail.python.org/mailman/

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

2017-01-06 Thread Paul Rubin
Peter Otten <__pete...@web.de> writes: > How would you implement stopmin()? Use itertools.takewhile -- https://mail.python.org/mailman/listinfo/python-list

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

2017-01-06 Thread Wolfgang Maier
On 1/6/2017 15:04, Peter Otten wrote: Example: you are looking for the minimum absolute value in a series of integers. As soon as you encounter the first 0 it's unnecessary extra work to check the remaining values, but the builtin min() will continue. The solution is a minimum function that allo

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

2017-01-06 Thread Jussi Piitulainen
Peter Otten writes: > Example: you are looking for the minimum absolute value in a series of > integers. As soon as you encounter the first 0 it's unnecessary extra work > to check the remaining values, but the builtin min() will continue. > > The solution is a minimum function that allows the u

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

2017-01-06 Thread Peter Otten
Example: you are looking for the minimum absolute value in a series of integers. As soon as you encounter the first 0 it's unnecessary extra work to check the remaining values, but the builtin min() will continue. The solution is a minimum function that allows the user to specify a stop value: