all(iterable) should be like all(iterable, key=func)

functionality of all(iter) is to check all are true by converting it to boolean type if it is changed to all(iterable,key=lambda a:bool(a)) it still works and we can also do a lot of things like all([2,3,4],key=lamdba a:a) gives false above checks all are equal or not all([2,2,2],key=lambda a:a) gives true abouve checks all are equal or not all([1,2,4,9,key=lambda a:math.sqrt(a)**2==a]) and any(iter) should be changed as well

On Aug 22, 2015, at 23:31, shiva prasanth <kesavarapu.siva@gmail.com> wrote:
How could it possibly do that? You're calling a key function of one parameter that just returns its argument. That would have the same effect as passing no key at all (except for being a little slower). And, since a key function gets called once with each element, there's no easy way to turn all into a function that compares every element with all the other elements by adding a key function. I suppose if you really wanted to, you could write something like this: def make_cmp(): sentinel = first = object() def cmp(x): nonlocal first if first is sentinel: first = x return x == first return cmp all([1, 2, 3], key=make_cmp()) ... but that would be very silly. For a much simpler and more readable solution, see the unique_justseen recipe in itertools. If there's a second unique value, the elements weren't all equal.
Since math.sqrt returns a float, this will be true for almost all integers. (I'm not sure whether very big ones could fail because of rounding errors.) So, none of your examples actually work. And, if you came up with one that did, why wouldn't you just use a generator expression? Which of these is more readable: all(x>0 for x in [1, 2, 3]) all([1, 2, 3], key = lambda x: x>0)

On Sun, Aug 23, 2015, at 02:31, shiva prasanth wrote:
No, it wouldn't... this doesn't make sense. If you want to check if they're all equal to the first element, you'd use: all([2, 3, 4], key=lambda a: a == 2)
all([2,2,2],key=lambda a:a) gives true abouve checks all are equal or not
all([1,2,4,9,key=lambda a:math.sqrt(a)**2==a])
I assume you want to check if something's a perfect square, you need int(math.sqrt(a)) in that case.

On Sun, Aug 23, 2015 at 12:01:57PM +0530, shiva prasanth wrote:
There is no need for the lambda. key=bool will work the same way, and more efficiently.
we can also do a lot of things like all([2,3,4],key=lamdba a:a) gives false
No, it would return True, since all the items are truthy. lambda a: a is equivalent to not transforming the items at all, which makes it equivalent to using bool as the implied key. You can test that yourself by using: f = lambda a: a all(f(a) for a in [2, 3, 4]) There is no need for a key function, since we can get the same result using either map() or a generator expression: all(map(keyfunction, iterable)) all(keyfunction(obj) for obj in iterable) whichever you prefer. There's no obvious one-liner to check whether an iterable contains only the same object, but a helper function is easy to write: def same(iterable): it = iter(iterable) try: first = next(it) except StopIteration: return False return all(obj == first for obj in it) -- Steve

On Aug 22, 2015, at 23:31, shiva prasanth <kesavarapu.siva@gmail.com> wrote:
How could it possibly do that? You're calling a key function of one parameter that just returns its argument. That would have the same effect as passing no key at all (except for being a little slower). And, since a key function gets called once with each element, there's no easy way to turn all into a function that compares every element with all the other elements by adding a key function. I suppose if you really wanted to, you could write something like this: def make_cmp(): sentinel = first = object() def cmp(x): nonlocal first if first is sentinel: first = x return x == first return cmp all([1, 2, 3], key=make_cmp()) ... but that would be very silly. For a much simpler and more readable solution, see the unique_justseen recipe in itertools. If there's a second unique value, the elements weren't all equal.
Since math.sqrt returns a float, this will be true for almost all integers. (I'm not sure whether very big ones could fail because of rounding errors.) So, none of your examples actually work. And, if you came up with one that did, why wouldn't you just use a generator expression? Which of these is more readable: all(x>0 for x in [1, 2, 3]) all([1, 2, 3], key = lambda x: x>0)

On Sun, Aug 23, 2015, at 02:31, shiva prasanth wrote:
No, it wouldn't... this doesn't make sense. If you want to check if they're all equal to the first element, you'd use: all([2, 3, 4], key=lambda a: a == 2)
all([2,2,2],key=lambda a:a) gives true abouve checks all are equal or not
all([1,2,4,9,key=lambda a:math.sqrt(a)**2==a])
I assume you want to check if something's a perfect square, you need int(math.sqrt(a)) in that case.

On Sun, Aug 23, 2015 at 12:01:57PM +0530, shiva prasanth wrote:
There is no need for the lambda. key=bool will work the same way, and more efficiently.
we can also do a lot of things like all([2,3,4],key=lamdba a:a) gives false
No, it would return True, since all the items are truthy. lambda a: a is equivalent to not transforming the items at all, which makes it equivalent to using bool as the implied key. You can test that yourself by using: f = lambda a: a all(f(a) for a in [2, 3, 4]) There is no need for a key function, since we can get the same result using either map() or a generator expression: all(map(keyfunction, iterable)) all(keyfunction(obj) for obj in iterable) whichever you prefer. There's no obvious one-liner to check whether an iterable contains only the same object, but a helper function is easy to write: def same(iterable): it = iter(iterable) try: first = next(it) except StopIteration: return False return all(obj == first for obj in it) -- Steve
participants (4)
-
Andrew Barnert
-
random832@fastmail.us
-
shiva prasanth
-
Steven D'Aprano