[New-bugs-announce] [issue14961] map() and filter() methods for iterators

Vladimir Berkutov report at bugs.python.org
Wed May 30 10:59:54 CEST 2012


New submission from Vladimir Berkutov <dair.targ at gmail.com>:

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).

----------
components: Interpreter Core, Library (Lib)
messages: 161935
nosy: dair-targ
priority: normal
severity: normal
status: open
title: map() and filter() methods for iterators
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14961>
_______________________________________


More information about the New-bugs-announce mailing list