Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Fap fap fap

Name: Anonymous 2007-04-04 9:44 ID:HirEo2Uj


import operator

def Eval(l):
    if type(l) == list:
        if not l:
            return None
        else:
            return Eval(l[0])(*l[1:])
    else:
        return l

#Decorator to evaluate arguments; they default to lazy evaluation
evalargs = lambda f: lambda *a: f(*map(Eval, a))

add = evalargs(lambda *a: reduce(operator.add, a))

def eq(*a):
    l = len(a)
    if l < 2:
        return True
    o = Eval(a[0])
    for i in xrange(1, l):
        if Eval(a[i]) != o:
            return False
    return True

def If(cond, t, f):
    if Eval(cond): return Eval(t)
    else: return Eval(f)

def Do(*a):
    for i in a:
        out = Eval(i)
    return out
#Maybe also Do = lambda *a: map(Eval, a)[-1], but less efficient

@evalargs
def OutLn(*a):
    print u''.join(unicode(i) for i in a)


#Ok, now let's test it:
body = [If, [eq, 1, 1], [OutLn, 10], [OutLn, 20]]
Eval(body)
#Expected: prints 10


Do you think I've fapped enough, or should I fap further into this?

Name: Anonymous 2007-04-04 13:43 ID:KDfc/km0

>>2
Faptastic! Though my Eval supports any data type and any function present in the langauge, for which arguments default to lazy evaluation (I've written an evalargs decorator to do otherwise), my eq supports any number of expressions, and I've implemented Do but not used it in the example. I could go on and see how I can wrap Eval in functions to get nested scopes.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List