A Lambda is an anonymous function. Places like AWS have services set up entirely just to host these simple functions that don't require a ton of code or backend.
To run the Python here, I go back to Google Colaboratory; Check out my Lambda book here
Ex. 1
g = lambda x: 3*x + 2
g(3)
Simple.
Ex. 2
full_name = lambda fn, ln: fn.strip().title() + " " + ln.strip().title()
full_name(" Ember", "Serros")
Notice the spaces in front of the first name (' Ember')
Ex. 3
def func(x,y):
"""Does things"""
func2 = lambda x: x + y+3
print(func(2,7))
Using def seems to be hit or miss. This returns None, which technically is correct,
but I'm expecting a number (12). It's missing a return statement;
return func2(x) + 3
Ex. 4
def func(x,y):
funct2 = lambda x: x+y+5
return funct2(x) + 6
print(func(3,78))
Works just fine.
Ex. 5
def bqf(a,b,c):
frog = lambda x: a*x**2 + b*x + c
return frog(c) + 6
print(bqf(5,5,1))
Also works.
Comments
Post a Comment