Python Lambda Forms

From Schmid.wiki
Jump to: navigation, search

Simple Lambda Forms

def evaluate(f):
    print f(2)
f = lambda x: x**2
print evaluate(f)      <- prints 4
def evaluate2(f):
    print f(2,3)
f = lambda a,b: a+b
print evalute2(f)      <- prints 5

This example is adopted from one of the Python ASCII Art pages:

 
 def use(f):
     print f(x,l)

 shade     = lambda col,x,y: col - float(w-x) / w
 righthalf = lambda colf,x,y: (x>(w/2)) * col
 
 wave    = lambda col,x,y: col + sin( y * .2 ) / 10 + .5
 int2    = lambda col,x,y: col + (x * y) % 40 / 40. - .2
 stripe  = lambda col,x,y: col + (x + y) % 10 / 10. - .2
 circles = lambda col,x,y: col + (((x-2*w/3)**2 + (y*1.8-43)**2)**.5) % 8 < 4
 int1    = lambda col,x,y: col + ( sin( x / 8.) + 2 + sin( y * x / 2.)) ** ( sin( y / 7. ) + 1 ) / 16.
 blob    = lambda col,x,y: col + ( sin( x * .5 + sin(  x * .23 + y * .21 ))
                       + sin( y * .5 + sin( -x * .23 + y * .21 ))) / 2. + .2

 combination = lambda x,y : shade(circles(0,x,y)/4.+blob(0,x,y)*0.5+0.3,x,y)
 
 use(combination)

This doesn't seem very clever. Any comments on this are very welcome.

Personal tools