[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    [Python-ideas] In consistent arguments: map v filter [was: Enhancing iterators ...]
From:       "Stephen J. Turnbull" <stephenjturnbull () gmail ! com>
Date:       2021-11-23 8:47:31
Message-ID: m235nn8di4.fsf () sk ! tsukuba ! ac ! jp
[Download RAW message or body]

Chris Angelico writes:

 > It's not too hard to create your own dataflow class if you want one.
 > It can start with any arbitrary iterable, and then have your map and
 > filter methods just the same.

One thing I noticed when implementing this class (yours is better, so
I'm not posting mine :-) is that you and I both implemented .map in
the obvious way for this use case, but the map function takes multiple
iterables.  On the other hand, filter takes only one iterable
argument.

Obviously, you can implement multifilter

def multifilter(func, *iterables):
    filter(lambda x: func(*x), zip(*iterables))

I think generalizing to this is a YAGNI, since it's so simple.

Also, returning an iterable of tuples may not be the right thing.
That is, you might want it to return a tuple of iterables, but that
would be messy to implement, and in general can't be done
space-efficiently I think.  This apparently is a "no one ever needed
it."

Changing map to take a sequence of iterables is a non-starter, since
that would be backward incompatible.

There's also implementing zip's strict argument, eg,

def zippymap(func, *iterables, strict=False):
    return map(lambda x: func(*x), zip(*iterables, strict))

and corresponding zippymappers for any other mappers (including
filter).  This seems like it might be useful extension to the
functions in the stdlib for the same reason that it's useful for zip
itself.  Even though it's so easy to implement in terms of zip, it
would be more discoverable as a documented argument to the functions.

Comments?

Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/DD6CKRBO7CTIT6SLBPON3CUKQYQE6OYG/
 Code of Conduct: http://python.org/psf/codeofconduct/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic