homepage

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: map() and filter() methods for iterators
Type: enhancement Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Ramchandra Apte, dair-targ, lehmannro, ncoghlan
Priority: normal Keywords:

Created on 2012年05月30日 08:59 by dair-targ, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (6)
msg161935 - (view) Author: Vladimir Berkutov (dair-targ) Date: 2012年05月30日 08:59
It might be useful to introduce a new map() and filter() methods to iterators and iterables. Both methods should accept lambda/function which transforms a single argument into value. Both methods should return another iterator.
# proposed methods usage:
range(10).map(abs).filter(lambda x: x % 5 == 0)
# existing equivalent:
filter(lambda x: x % 5 == 0, map(abs, range(-10, 10)))
# result:
[10, 5, 0, 5]
Rough equivalent of implementation:
class iterator:
 def map(self, fn):
 for v in self:
 yield fn(v)
 
 def filter(self, fn):
 for v in self:
 if fn(v):
 yield v
 else:
 continue
Introduction of such methods will allow to transform collections lazy without significant memory consumption (as was mentioned in http://bugs.python.org/issue912738).
msg161937 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2012年05月30日 09:07
I think this is quite a major change to Python and this needs a PEP BTW issue912738 and therefore this bug no longer applies to Python 3.
In Python 3 map returns an iterator.
Even if you wanted to implement this feature in Python 2, it is not possible because in Python 2.7 (the only Python 2 release which is not in bug-fix mode) only minor enhancements are allowed.
-1 for this proposal
msg161938 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2012年05月30日 09:08
Sorry, small mistake.
Actually all the other Python 2.x releases are in security-fix mode.
msg161939 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2012年05月30日 09:09
Sorry,
To clarify:
Python 2.7 is in bug-fix mode which means only minor enhancements are allowed.
msg161940 - (view) Author: Robert Lehmann (lehmannro) * Date: 2012年05月30日 09:11
Your proposal seems two-fold: (a) make map/filter lazy and (b) have them as methods instead of functions.
It seems Tim borrowed Guido's time machine and already implemented (a) in Python 3.x, see http://docs.python.org/py3k/library/functions.html#map and http://docs.python.org/py3k/library/functions.html#filter.
Your second proposal-- which is better suited for python-ideas, really --is obstructed by iterators being merely a protocol (the next/__next__ method) which makes it hard to add those methods to one particular type. (This very discussion pops up every so often for str.join too.)
I'd recommend closing this issue.
msg161943 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2012年05月30日 11:01
As Robert noted, the map() and filter() builtins in Python 3 are already lazy and there's no reason to expand the iterator protocol for this functionality.
Map and filter also have dedicated syntax in the form of comprehensions and generator expressions:
 itr = (x for x in map(abs, range(10)) if x % 5 == 0)
Furthermore, the standard library already provides an entire module of tools for creating and working with lazy iterators in both Python 2 and Python 3: http://docs.python.org/library/itertools 
History
Date User Action Args
2022年04月11日 14:57:31adminsetgithub: 59166
2012年05月30日 11:01:16ncoghlansetstatus: open -> closed

nosy: + ncoghlan
messages: + msg161943

resolution: rejected
2012年05月30日 09:11:00lehmannrosetnosy: + lehmannro
messages: + msg161940
2012年05月30日 09:09:27Ramchandra Aptesetmessages: + msg161939
2012年05月30日 09:08:30Ramchandra Aptesetmessages: + msg161938
2012年05月30日 09:07:15Ramchandra Aptesetnosy: + Ramchandra Apte
messages: + msg161937
2012年05月30日 08:59:54dair-targcreate

AltStyle によって変換されたページ (->オリジナル) /