Created
March 30, 2013 05:25
-
-
Save 89465127/5275506 to your computer and use it in GitHub Desktop.
A couple ways to use map() when you need nested function calls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Core functions | |
def triple(num): | |
return num*3 | |
def double(num): | |
return num*2 | |
# Input values | |
nums = [1,2,3,4,5,6] | |
# With lambda | |
def use_lambda(): | |
print map(lambda x: triple(double(x)), nums) | |
# With an itermediate "composite" function | |
def composite_function(num): | |
return triple(double(num)) | |
def use_composite_function(): | |
print map(composite_function, nums) | |
# Entry point compares both valid ways of using map() | |
if __name__ == '__main__': | |
use_lambda() | |
use_composite_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment