- #!/usr/bin/python
- print "\nlook at all the little lambies ..."
- print ', '.join(["baa-baa" for x in range(5)])
- #Lambdas,
- print "\n---| version 1 |----------------"
- x=1
- def func1(y,lam = lambda x: int(x)):
- print("(float) x + y = ", x+y)
- print("(int) x + y = ",lam(x+y))
- print func1(2.5)
- print "\n#--| version 2 |----------------"
- print "# cleaner prints"
- x=1
- def func2(y,lam = lambda x: int(x)):
- print "(float) x + y = ", x+y
- print "(int) x + y = ",lam(x+y)
- print func2(2.5)
- print "\n#--| version 3 |----------------"
- print "# using returns"
- x=1
- def func3(y, lam = lambda x : int(x)):
- return x + lam(y)
- print func3(2.5), " # print with default"
- print func3(2.5,float), "# print with float"
- print func3(2.5,int), " # print with int (same as default)"
- print "\n# --| version 4 |----------------------"
- print "# same functionality, pure functions (no globals)"
- def mysum(x,y=0, filter = lambda x : int(x)):
- return filter(x+y)
- print mysum(1,2.5), " # print with default"
- print mysum(1,2.5,float), "# print with float"
- print mysum(1,2.5,int), " # print with int (same as default)"
- print "\n# --| version 5 |----------------------"
- print "# exercising mysum"
- myFilter = lambda x : int(x) if x % 1 == 0.0 else float(x)
- print mysum(2, 5.3), " # floats coerced to an int"
- print mysum(2.5,2.5,myFilter), " # two floats that sum to a whole num"
- print mysum(1,2.5,myFilter), "# two things that sum to an un-wholy number"
- print "\n# --| version 6 |----------------------"
- print "# as above, cleaner"
- def maybeInt(x):
- return int(x) if x % 1 == 0.0 else float(x)
- print mysum(2, 5.3), " # floats coerced to an int"
- print mysum(2.5,2.5,maybeInt), " # two floats that sum to a whole num"
- print mysum(1,2.5,maybeInt), "# two things that sum to an un-wholy number"