Favorite non-python language trick?

George Sakkis gsakkis at rutgers.edu
Sat Jun 25 15:17:20 EDT 2005


"Konstantin Veretennicov" <kveretennicov at gmail.com> wrote:

> On 6/25/05, Steven D'Aprano <steve at removethiscyber.com.au> wrote:
> > On Sat, 25 Jun 2005 17:41:58 +0200, Konstantin Veretennicov wrote:
> >
> > > On 6/25/05, Mandus <mandus at gmail.com> wrote:
> > >> It is really a consensus on this; that
> > >> removing map, filter, reduce is a good thing? It will render a whole lot
> > >> of my software unusable :(
> > >
> > > I think you'll be able to use "from __past__ import map, filter,
> > > reduce" or something like that :) They don't have to be built-in.
> >
> > More likely they will be moved to something like itertools than "__past__".
> >
> > Or just define them yourself:
> >
> > def map(f, seq):
> >         return [f(x) for x in seq]
> >
> > def filter(p, seq):
> >     return [x for x in seq if p(x)]
> >
> > def reduce(f, seq, zero):
> >     r = zero
> >     for x in seq: r = f(r, x)
> >     return r
>
> FWIW, these don't exactly reproduce behaviour of current built-ins.
> Filter, for instance, doesn't always return lists and map accepts more
> than one seq... Just my $.02.
>
> - kv

If they go to itertools, they can simply be:

def map(f, *iterables):
    return list(imap(f,*iterables))

def filter(f, seq): 
    return list(ifilter(f,seq))

George




More information about the Python-list mailing list