Created
February 16, 2016 22:14
-
-
Save BrandonLMorris/2143982da69082987116 to your computer and use it in GitHub Desktop.
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
""" | |
Simple example of currying and creating higher-order functions | |
in Python. | |
Taken from | |
https://stackoverflow.com/questions/24881604/when-should-i-use-function-currying-in-python | |
""" | |
def curry2(f): | |
""" | |
Returns a function g such that g(x)(y) == f(x, y) | |
>>> from operator import add | |
>>> add_three = curry2(add)(3) | |
>>> add_three(4) | |
""" | |
def g(x): | |
def h(y): | |
return f(x, y) | |
return h | |
return g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment