Skip to content

Instantly share code, notes, and snippets.

@89465127
Created March 30, 2013 05:25
Show Gist options
  • Save 89465127/5275506 to your computer and use it in GitHub Desktop.
Save 89465127/5275506 to your computer and use it in GitHub Desktop.
A couple ways to use map() when you need nested function calls.
# 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