any() and all() shorthand

C

castironpi

any( iterab ) and all( iterab )

as shorthand for reduce( operator.or_, iterab ) and
reduce( operator.and_, iterab ).

What do you think?
 
G

Guilherme Polo

2008/1/7 said:
any( iterab ) and all( iterab )

as shorthand for reduce( operator.or_, iterab ) and
reduce( operator.and_, iterab ).

What do you think?

You are too late, any and all are built-in into python 2.5
 
C

castironpi

You are too late, any and all are built-in into python 2.5

Hi, excellent. Now how about something more generic, possibly:

[ x.y() for x or _next_ in c ]

where the context of _next_ is limited in complexity, and/or can only
occur in a generator?
 
G

Guilherme Polo

2008/1/7 said:
You are too late, any and all are built-in into python 2.5

Hi, excellent. Now how about something more generic, possibly:

[ x.y() for x or _next_ in c ]

where the context of _next_ is limited in complexity, and/or can only
occur in a generator?

Would you care to explain what that syntax supposedly means ? By
_next_ you mean something like the next method in generators ? _next_
executes if x is false ? so whatever _next_ returns is named as x, so
you can call x.y() ? I really didn't get your new syntax inside that
list comprehension, neither its uses.
 
C

castironpi

Hi, excellent. Now how about something more generic, possibly:
[ x.y() for x or _next_ in c ]
where the context of _next_ is limited in complexity, and/or can only
occur in a generator?

Would you care to explain what that syntax supposedly means ? By
_next_ you mean something like the next method in generators ? _next_
executes if x is false ? so whatever _next_ returns is named as x, so
you can call x.y() ? I really didn't get your new syntax inside that
list comprehension, neither its uses.

The idea is a shorthand for reduce. Here, _next_ meant the next item
in the iterable c.
 
C

castironpi

The idea is a shorthand for reduce. Here, _next_ meant the next item
in the iterable c.

'Only' is another known quantifier in logic: 'all and only'. Any
(there exists) and all (for all) are too. 'Only' (and not others)
could be useful, from the theoretical standpoint. But where?
 
T

Tim Chase

The idea is a shorthand for reduce. Here, _next_ meant the next item
in the iterable c.

You mean like one of these:

def lookahead(iterator):
i = iter(iterator)
x = i.next()
for item in i:
yield x, item
x = item

def lookahead2(iterator, **kwarg):
i = iter(iterator)
if 'initial' in kwarg:
x = kwarg['initial']
else:
x = i.next()
for item in i:
yield x, item
x = item
if 'last' in kwarg:
yield x, kwarg['last']

print 'lookahead()'
for this, next in lookahead([1,2,3,4,5]):
print this, next

print 'lookahead2()'
for this, next in lookahead2([1,2,3,4,5]):
print this, next

print 'lookahead2(initial=42)'
for this, next in lookahead2([1,2,3,4,5], initial=42):
print this, next

print 'lookahead2(last=42)'
for this, next in lookahead2([1,2,3,4,5], last=42):
print this, next

print 'lookahead2(initial=3.14159, last=42)'
for this, next in lookahead2([1,2,3,4,5],
initial=3.14159, last=42):
print this, next


There are some alternate behaviors that can happen at the end
points, so depending on which behavior you want, the lookahead()
is cleanest, but doesn't allow you to handle edge cases. The
lookahead2() is a little more complex, but allows you to specify
a first item for pairing (so "next" touches every item in your
list) or a trailing element (so "this" touches every item).

-tkc
 
C

castironpi

print 'lookahead2(initial=3.14159, last=42)'
for this, next in lookahead2([1,2,3,4,5],
initial=3.14159, last=42):
print this, next

No, actually. But my mistake.

[ a.b() or _previous_ for a in c ]

means

1 or 2 or 3 or 4 or 5
where c= [ 1, 2, 3, 4, 5].

The mistake: this was not a list comprehension; I wanted to reduce to
a single value.

It's equivalent to reduce( operator.or_, [ 1, 2, 3, 4, 5 ] ), but
disnecessitates lambdas for slightly more complex reductions. But the
example is out of stock. Do we have one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top