Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: module1
- # Purpose:
- #
- # Notes: One of the major pieces of Python that lambda examples are applied to regularly are
- # Tkinter callbacks.
- # Basically, Pythonβs lambda is a tool for building functions (or more precisely,
- # function objects). That means that Python has two tools for building functions: def and lambda.
- #
- # References: http://www.blog.pythonlibrary.org/2010/07/19/the-python-lambda/
- # http://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/
- # http://www.bogotobogo.com/python/python_functions_lambda.php
- #
- # Author: User
- #
- # Created: 17/05/2014
- # Copyright: (c) User 2014
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- # Imports #
- import math
- # Main #
- def main():
- # Variables
- # Local
- #Global
- print(">>>Best if code is viewed in PyScripter. All code is directly from the above references,\
- a modification of their code or inspired by their code.<<<") %()
- print
- print(">>>Normal and lambda funcs comparison<<<")
- for i in range(49, 65, 15): ## Loops thru two squares for test purposes
- print "Using math func, root of %i is %.2f" %(i, f_sqroot(i)) ## Prints result of call to normal func
- print "Using lambda func, root of %i is %.2f" %(i, k_square_rt(i)) ## Prints result of call to lambda func
- print
- print ("Call to k_sum, lambda, func, is %i") %k_sum(3, 4) ## Calls lambda sum kfunc
- print
- # Lambda in a 'jump tabel' for actually referencing a lambda func in a list
- print(">>>Using lambda funcs from a list, a 'jump table'<<<")
- print(">>>I.e. you can select an expression from a list based on some criteria. Cool!<<<") %()
- print("Create expression list and reference and use based on list index value")
- k_LambdaTest = [lambda x: x ** 2, lambda x: x ** 3, lambda x: x ** 4]
- print("Here we cycle thru each lambda in the list and print results")
- for i in range(len(k_LambdaTest)): ## Uses length of lambda object to set up iteration
- print("Lambda func %i is %i") %(i, k_LambdaTest[i](3)) ## Cycles thru each lambda func with 3 and prints result
- print("Here we only ref the first lambda in list and print results.")
- print("First lambda func result is: %i") %k_LambdaTest[0](3) ## Calls the first lambda func in list with 3
- print
- print(">>>Use lambda to select from a list without for loop<<<")
- k_Min = (lambda x, y: x if x < y else y) ## Defines lambda kfunc
- print("Min is %i") %k_Min(101*99, 102*98) ## Uses kfunc to print min of list
- print("Min is %i") %k_Min(102*98, 101*99) ## Uses kfunc to print min of list
- print
- print(">>>Normal vs inline lambda funcs<<<")
- x = 2
- print("Normal cube func of %i: %i") %(x, f_Cube(x))
- k_Lamb = lambda x: x ** 3
- print ("Lambda cube func of %i produces: %i") %(x, k_Lamb(x))
- print
- # Lambda used to modify the expression in a norm func
- print(">>>Lambda being used to modify an expression in a norm func<<<")
- k_ModSum = f_Make_Incrementor(2) ## Calls the norm func with the value for the lambda func expression
- k_ModSum2 = f_Make_Incrementor(6) ## Calls the norm func with the value for the lambda func expression
- print("First lambda useing a base of 2 (x + 2) and x of 42: %i") %(k_ModSum(42))
- print("Second lambda useing a base of 6 (x + 6) and x of 42: %i") %(k_ModSum2(42))
- print
- # Lambda used to inline filter a list
- # Produce prime list
- print(">>>Lambda being used to quickly create a 2 to 49 'sieve of Eratosthenes' prime list<<<")
- v_Nums = range(2, 50) ## Range to 'sieve primes out of
- for i in range(2, 8): ## Sieve range, 2 to sqrt of 49
- v_Nums = filter(lambda x: x == i or x % i, v_Nums) ## Instead of having to loop thru each list item
- ## and filter each out with if/then, we filter inline
- ## with lambda
- print v_Nums ## Print 2 to 49 prime list
- print
- # Produce odd list
- print(">>>Lambda being used to quickly create an odds list<<<")
- v_List = range(2, 50)
- for i in v_List:
- v_List = filter(lambda x: x % 2, v_List)
- print("Odd List %s") %(v_List)
- print
- # Lambda and map
- print(">>>Moding a list using trad., map and func, and map and lambda<<<")
- # Traditional way to modify each item in a list
- print(">>>Lambda and map<<<")
- items = [1, 2, 3, 4, 5]
- squared = []
- for x in items:
- squared.append(x ** 2)
- print ("Traditional way: %s") %squared
- # Using a traditional func and 'Map' to modify each item in a list
- def f_Sqr(x): return x ** 2 ## Function
- print("With func and map: %s") %list(map(f_Sqr, items))## 'Map' applies the func to each item in turn without
- ## explicitly using a for loop
- print("With map and lambda: %s") %list(map((lambda x: x **2), items)) ## Instead of a func just put the lambda func
- ## inside the map, no for loop needed
- # Tests
- # Output to file
- ## v_Path = 'C:/Users/User/Documents/out.txt'
- ## v_Mode = 'w'
- ## v_FileConn = f_OutputToFileOut(v_Path, v_Mode)
- ## sys.exit("Error message") ## Stop execution
- #------------------------------
- # Functions #
- # Normal Functions
- # Make a modifiable func using lambda
- def f_Make_Incrementor(n):
- return lambda x: x + n ## returns the modified by (n) lambda func
- # Cube func
- def f_Cube(x):
- return x ** 3 ## Returns the result of the evaluated expression
- # Square root func
- def f_sqroot(x): # Start func
- return math.sqrt(x) ## Returns result of call to math.sqrt()
- # Generator Functions
- def g_TestGen():
- pass
- # Lambda Functions
- # Square root lambda func
- k_square_rt = lambda x: math.sqrt(x) ## Creates and obj, square_rt, that calls the math.sqrt() func
- ## def x: return math.sqrt(x)
- k_sum = lambda x, y: x + y ## def sum(x,y): return x + y
- # Classes #
- # Main Loop #
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement