Created
March 10, 2017 12:07
-
-
Save harunyasar/f3b94a1b52c7b9e71623951738099381 to your computer and use it in GitHub Desktop.
Example of function composition with Python
This file contains 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
def addition(x): | |
return x + 5 | |
def subtraction(y): | |
return y - 1 | |
def compose(*funcs): | |
return lambda x: reduce(lambda v, f: f(v), reversed(funcs), x) | |
c = compose(addition, subtraction) | |
print(c(10)) # 14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment