map/filter/reduce/lambda opinions and background unscientific mini-survey

Sean McIlroy sean_mcilroy at yahoo.com
Fri Jul 1 19:07:20 EDT 2005


Tom Anderson wrote:
<snip>
> So, if you're a pythonista who loves map and lambda, and disagrees with
> Guido, what's your background? Functional or not?

glad you asked. personally i don't know lisp (or scheme), but now i've
decided to learn it, because eventually it will no longer be possible
in python to pass functions as arguments or return them as values. the
education sig will have to change its motto to "computer programming
for every C-programmer". until then hangers-on like myself can use
home-grown substitutes for the functional constructs (examples below),
but in my opinion the best thing is to migrate as soon as possible. the
real programmers are squeezing us out. now is the time to abandon
python for an intelligent language (macros! real conditional evaluation
instead of the and/or kluge!)

def LISTCOMP(f,s,g):
    reval = []
    for x in s:
        if g(x):
            reval.append(f(x))
    return reval

def LAMBDA(arguments,value):
    symbols = arguments.split(',')
    def reval(*args):
        for i in range(len(args)):
            locals()[symbols[i]] = args[i]
        return eval(value)
    return reval

def MAP(f,s):
    return LISTCOMP(f,s,LAMBDA('x','True'))

def FILTER(f,s):
    return type(s)(LISTCOMP(LAMBDA('x','x'),s,f))

def REDUCE(f,s,t):
    if not s: return t
    return f(s[0],REDUCE(f,s[1:],t))




More information about the Python-list mailing list