Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
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