Parametrized any() and all() ?

Hello any() and all() are very useful small functions, and I am wondering if it could be interesting to have them work with different operators, by using a callable. e.g. something like: import operator def any(iterable, filter=operator.truth): for element in iterable: if filter(element): return True return False For instance I could then us any() to find out if there's a None in the sequence: if any(iterable, op=lambda x: x is None): raise SomeError("There's a none in that list") Granted, it's easy to do it myself in a small util function - but since any() and all() are in Python... Cheers Tarek -- Tarek Ziadé · http://ziade.org · @tarek_ziade

Hey Tarek, I would write that as any(x is None for x in it) -- the example you gave doesn't really strike me as an improvement over that, although I could see how many there are cases where it's nicer... On Wed, Jan 16, 2013 at 11:30 AM, Tarek Ziadé <tarek@ziade.org> wrote:
Hello
any() and all() are very useful small functions, and I am wondering if it could be interesting to have them work with different operators, by using a callable.
e.g. something like:
import operator
def any(iterable, filter=operator.truth): for element in iterable: if filter(element): return True return False
For instance I could then us any() to find out if there's a None in the sequence:
if any(iterable, op=lambda x: x is None): raise SomeError("There's a none in that list")
Granted, it's easy to do it myself in a small util function - but since any() and all() are in Python...
Cheers Tarek
-- Tarek Ziadé · http://ziade.org · @tarek_ziade
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- cheers lvh

I think adding this example to docstring and document may help many people. On Wed, Jan 16, 2013 at 7:33 PM, Laurens Van Houtven <_@lvh.cc> wrote:
Hey Tarek,
I would write that as any(x is None for x in it) -- the example you gave doesn't really strike me as an improvement over that, although I could see how many there are cases where it's nicer...
On Wed, Jan 16, 2013 at 11:30 AM, Tarek Ziadé <tarek@ziade.org> wrote:
Hello
any() and all() are very useful small functions, and I am wondering if it could be interesting to have them work with different operators, by using a callable.
e.g. something like:
import operator
def any(iterable, filter=operator.truth): for element in iterable: if filter(element): return True return False
For instance I could then us any() to find out if there's a None in the sequence:
if any(iterable, op=lambda x: x is None): raise SomeError("There's a none in that list")
Granted, it's easy to do it myself in a small util function - but since any() and all() are in Python...
Cheers Tarek
-- Tarek Ziadé · http://ziade.org · @tarek_ziade
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- cheers lvh
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- INADA Naoki <songofacandy@gmail.com>

On 1/16/13 11:33 AM, Laurens Van Houtven wrote:
Hey Tarek,
I would write that as any(x is None for x in it)
But here you're building yet another iterable to adapt it to any(), which seems to me overkill if we can just parametrized the loop in any() Cheers Tarek -- Tarek Ziadé · http://ziade.org · @tarek_ziade

On 2013-01-16, at 11:44 , Tarek Ziadé wrote:
On 1/16/13 11:33 AM, Laurens Van Houtven wrote:
Hey Tarek,
I would write that as any(x is None for x in it)
But here you're building yet another iterable to adapt it to any(), which seems to me overkill if we can just parametrized the loop in any()
It's just a generator, and will be terminated early if possible. I'm pretty sure adding a key function to any and all has already been submitted several times, and from what I remember it was struck down every time because the use case is covered by Laurens's suggestion: key functions are necessary when you'd otherwise need DSU (because the result is the original input, not the key function's output) but it's not the case for any() and all() Here's the previous/latest instance: http://mail.python.org/pipermail/python-ideas/2012-July/015837.html

On Wed, Jan 16, 2013 at 8:44 PM, Tarek Ziadé <tarek@ziade.org> wrote:
On 1/16/13 11:33 AM, Laurens Van Houtven wrote:
Hey Tarek,
I would write that as any(x is None for x in it)
But here you're building yet another iterable to adapt it to any(), which seems to me overkill if we can just parametrized the loop in any()
Such a micro-optimization isn't worth the cost of adding a second way to do it that everyone will then need to learn. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 16/01/13 22:10, Nick Coghlan wrote:
On Wed, Jan 16, 2013 at 8:44 PM, Tarek Ziadé<tarek@ziade.org> wrote:
On 1/16/13 11:33 AM, Laurens Van Houtven wrote:
Hey Tarek,
I would write that as any(x is None for x in it)
But here you're building yet another iterable to adapt it to any(), which seems to me overkill if we can just parametrized the loop in any()
Such a micro-optimization isn't worth the cost of adding a second way to do it that everyone will then need to learn.
For all we know, adding a filter function will be a pessimization, not an optimization, using more memory and/or being slower than using a generator expression. It certainly isn't clear to me that creating a generator expression like (x is None for x in it) is more expensive than creating a filter function like (lambda x: x is None). -1 on adding a filter function. -- Steven

On 1/16/13 3:10 PM, Steven D'Aprano wrote:
On 16/01/13 22:10, Nick Coghlan wrote:
On Wed, Jan 16, 2013 at 8:44 PM, Tarek Ziadé<tarek@ziade.org> wrote:
On 1/16/13 11:33 AM, Laurens Van Houtven wrote:
Hey Tarek,
I would write that as any(x is None for x in it)
But here you're building yet another iterable to adapt it to any(), which seems to me overkill if we can just parametrized the loop in any()
Such a micro-optimization isn't worth the cost of adding a second way to do it that everyone will then need to learn.
For all we know, adding a filter function will be a pessimization, not an optimization, using more memory and/or being slower than using a generator expression. It certainly isn't clear to me that creating a generator expression like (x is None for x in it) is more expensive than creating a filter function like (lambda x: x is None).
-1 on adding a filter function.
I abandoned the idea, but I'd be curious to understand how creating several iterables with one that has an 'if', can be more efficient than having a single iterable with an 'if'...
-- Tarek Ziadé · http://ziade.org · @tarek_ziade

On 16 Jan, 2013, at 15:52, Tarek Ziadé <tarek@ziade.org> wrote:
On 1/16/13 3:10 PM, Steven D'Aprano wrote:
On 16/01/13 22:10, Nick Coghlan wrote:
On Wed, Jan 16, 2013 at 8:44 PM, Tarek Ziadé<tarek@ziade.org> wrote:
On 1/16/13 11:33 AM, Laurens Van Houtven wrote:
Hey Tarek,
I would write that as any(x is None for x in it)
But here you're building yet another iterable to adapt it to any(), which seems to me overkill if we can just parametrized the loop in any()
Such a micro-optimization isn't worth the cost of adding a second way to do it that everyone will then need to learn.
For all we know, adding a filter function will be a pessimization, not an optimization, using more memory and/or being slower than using a generator expression. It certainly isn't clear to me that creating a generator expression like (x is None for x in it) is more expensive than creating a filter function like (lambda x: x is None).
-1 on adding a filter function.
I abandoned the idea,
but I'd be curious to understand how creating several iterables with one that has an 'if', can be more efficient than having a single iterable with an 'if'...
Have you any reason to assume that "any(x is None for x in it)" is slow? I wouldn't be surprised if a key argument for any/all would have a higher overhead than the generator expression (if there is any difference). The key function would have to be called after all, with the overhead of normal function calls. Ronald
-- Tarek Ziadé · http://ziade.org · @tarek_ziade
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

On Wed, 16 Jan 2013 15:52:19 +0100 Tarek Ziadé <tarek@ziade.org> wrote:
For all we know, adding a filter function will be a pessimization, not an optimization, using more memory and/or being slower than using a generator expression. It certainly isn't clear to me that creating a generator expression like (x is None for x in it) is more expensive than creating a filter function like (lambda x: x is None).
-1 on adding a filter function.
I abandoned the idea,
but I'd be curious to understand how creating several iterables with one that has an 'if', can be more efficient than having a single iterable with an 'if'...
You know, discussing performance without posting benchmark numbers is generally pointless. Regards Antoine.

On 1/16/13 7:47 PM, Antoine Pitrou wrote:
You know, discussing performance without posting benchmark numbers is generally pointless.
Sure, yes, so I tried to implement it by adapting the current any() : http://tarek.pastebin.mozilla.org/2068630 but it is 20% slower in my benchmark. However, I have no idea if my implementation is the right way to do things. Cheers Tarek -- Tarek Ziadé · http://ziade.org · @tarek_ziade

On Fri, Jan 18, 2013 at 10:30 PM, Tarek Ziadé <tarek@ziade.org> wrote:
On 1/16/13 7:47 PM, Antoine Pitrou wrote:
You know, discussing performance without posting benchmark numbers is generally pointless.
Sure, yes, so I tried to implement it by adapting the current any() :
http://tarek.pastebin.mozilla.org/2068630
but it is 20% slower in my benchmark. However, I have no idea if my implementation is the right way to do things.
Resuming an existing frame (i.e. using a generator expression) is almost always going to be faster than going through the argument passing machinery and initialising a *new* frame. Chaining C level iterators together (e.g. map, itertools) is even faster. DSU is great for cases where you need it, but a transformation pipeline is otherwise likely to be faster (or at least not substantially slower). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Fri, Jan 18, 2013 at 5:52 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
DSU is great for cases where you need it, but a transformation pipeline is otherwise likely to be faster (or at least not substantially slower).
It took me a sec. :) DSU == "Decorate-Sort-Undecorate". [1] -eric [1] http://en.wikipedia.org/wiki/Decorate-sort-undecorate

On 1/18/2013 10:54 AM, Eric Snow wrote:
On Fri, Jan 18, 2013 at 5:52 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
DSU is great for cases where you need it, but a transformation pipeline is otherwise likely to be faster (or at least not substantially slower).
It took me a sec. :) DSU == "Decorate-Sort-Undecorate". [1]
No, no, no. Its Delaware State University in Dover, as opposed to Univesity of Delaware (UD) in Newark ;-). In other words, it depends on the universe you live in. -- Terry Jan Reedy

On Jan 19, 4:36 am, Terry Reedy <tjre...@udel.edu> wrote:
On 1/18/2013 10:54 AM, Eric Snow wrote:
It took me a sec. :) DSU == "Decorate-Sort-Undecorate". [1]
No, no, no. Its Delaware State University in Dover, as opposed to Univesity of Delaware (UD) in Newark ;-).
In other words, it depends on the universe you live in.
"Namespaces are one honking great idea" :)

On Sat, Jan 19, 2013 at 06:02:32PM -0800, alex23 <wuwei23@gmail.com> wrote:
On Jan 19, 4:36 am, Terry Reedy <tjre...@udel.edu> wrote:
On 1/18/2013 10:54 AM, Eric Snow wrote:
It took me a sec. :) DSU == "Decorate-Sort-Undecorate". [1]
No, no, no. Its Delaware State University in Dover, as opposed to Univesity of Delaware (UD) in Newark ;-).
In other words, it depends on the universe you live in.
"Namespaces are one honking great idea" :)
"In 1989, a random of the journalistic persuasion asked hacker Paul Boutin "What do you think will be the biggest problem in computing in the 90s?" Paul's straight-faced response: "There are only 17,000 three-letter acronyms." (To be exact, there are 26^3 = 17,576.)" Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

On 16 January 2013 10:30, Tarek Ziadé <tarek@ziade.org> wrote:
Hello
any() and all() are very useful small functions, and I am wondering if it could be interesting to have them work with different operators, by using a callable.
e.g. something like:
import operator
def any(iterable, filter=operator.truth): for element in iterable: if filter(element): return True return False
For instance I could then us any() to find out if there's a None in the sequence:
if any(iterable, op=lambda x: x is None): raise SomeError("There's a none in that list")
Granted, it's easy to do it myself in a small util function - but since any() and all() are in Python...
I wouldn't write a util function for this. The resulting code any(iterable, op=func) is not really shorter, easier or clearer than the current methods any(map(func, iterable)) any(func(x) for x in iterable) Oscar
participants (13)
-
alex23
-
Antoine Pitrou
-
Eric Snow
-
INADA Naoki
-
Laurens Van Houtven
-
Masklinn
-
Nick Coghlan
-
Oleg Broytman
-
Oscar Benjamin
-
Ronald Oussoren
-
Steven D'Aprano
-
Tarek Ziadé
-
Terry Reedy