Skip to content

Instantly share code, notes, and snippets.

@BrandonLMorris
Created February 16, 2016 22:14
Show Gist options
  • Save BrandonLMorris/2143982da69082987116 to your computer and use it in GitHub Desktop.
Save BrandonLMorris/2143982da69082987116 to your computer and use it in GitHub Desktop.
"""
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