any() and all() shorthand

Discussion in 'Python' started by castironpi@gmail.com, Jan 7, 2008.

  1. Guest

    any( iterab ) and all( iterab )

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

    What do you think?
     
    , Jan 7, 2008
    #1
    1. Advertising

  2. 2008/1/7, <>:
    > any( iterab ) and all( iterab )
    >
    > as shorthand for reduce( operator.or_, iterab ) and
    > reduce( operator.and_, iterab ).
    >
    > What do you think?
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


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

    --
    -- Guilherme H. Polo Goncalves
     
    Guilherme Polo, Jan 7, 2008
    #2
    1. Advertising

  3. Guest

    > 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?
     
    , Jan 7, 2008
    #3
  4. 2008/1/7, <>:
    > > 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.

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    -- Guilherme H. Polo Goncalves
     
    Guilherme Polo, Jan 7, 2008
    #4
  5. Guest

    On Jan 7, 1:29 pm, "Guilherme Polo" <> wrote:
    > 2008/1/7, <>:
    >
    > > > 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.
    >


    The idea is a shorthand for reduce. Here, _next_ meant the next item
    in the iterable c.
     
    , Jan 7, 2008
    #5
  6. Guest

    On Jan 7, 1:45 pm, wrote:
    > On Jan 7, 1:29 pm, "Guilherme Polo" <> wrote:
    >
    > 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?
     
    , Jan 7, 2008
    #6
  7. Tim Chase Guest

    > 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
     
    Tim Chase, Jan 7, 2008
    #7
  8. Guest

    > 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?
     
    , Jan 8, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. foreach shorthand

    , Mar 25, 2005, in forum: C++
    Replies:
    2
    Views:
    606
    =?iso-8859-1?Q?Ali_=C7ehreli?=
    Mar 25, 2005
  2. David W
    Replies:
    0
    Views:
    914
    David W
    Jan 17, 2007
  3. Foxpointe

    Shorthand for namespaces

    Foxpointe, Oct 31, 2006, in forum: XML
    Replies:
    4
    Views:
    443
    Foxpointe
    Oct 31, 2006
  4. Clint Olsen
    Replies:
    6
    Views:
    362
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Archos

    Shorthand operator for AND, NOT

    Archos, Dec 19, 2011, in forum: Javascript
    Replies:
    5
    Views:
    581
    Archos
    Dec 22, 2011
Loading...

Share This Page