This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add first() function that would take first matching case from an iterator
Type: Stage:
Components: None Versions: Python 3.1
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, gandalf, loewis, rhettinger
Priority: normal Keywords:

Created on 2009-06-29 21:20 by gandalf, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg89879 - (view) Author: Zbigniew Braniecki (gandalf) Date: 2009-06-29 21:20
Python has a very useful function any() that will iterate over a list
and break on the first matching case.

Now, it would be great to have similar function but with:
a) ability to use a callback for matching test
b) that would return the first match

so something like:

key = first(['es-AR','es','en-US'], lambda x:self._value.has_key(x))

instead of:

for k in ['es-AR','es','en-US']:
    if self._value.has_key(k):
        key = k
        break

It would be also nice to be able to capture the first match and return it:
value = first(['es-AR','es','en-US'], lambda x:self._value[x])

I'm not sure which one is more generically applicable.
msg89881 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-06-29 21:27
Please propose this on the python-ideas list first.
msg89882 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-06-29 21:33
Python already supports that, in the itertools module:

itertools.ifilter(lambda x:self._value.has_key(x),
                  ['es-AR','es','en-US']).next()
msg89886 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-06-29 22:21
We've already got one.  Thanks ;-)
History
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50628
2009-06-29 22:21:37rhettingersetnosy: + rhettinger
messages: + msg89886
2009-06-29 21:33:07loewissetnosy: + loewis
messages: + msg89882
2009-06-29 21:27:58benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg89881

resolution: rejected
2009-06-29 21:20:24gandalfcreate