Skip to content

Instantly share code, notes, and snippets.

@vlazzle
Created June 11, 2009 20:15
Show Gist options
  • Save vlazzle/128202 to your computer and use it in GitHub Desktop.
Save vlazzle/128202 to your computer and use it in GitHub Desktop.
#################################################
# currying and partial application in python #
# based on code from Simon Willison's geocoders #
#################################################
# curries fn, a function of exactly 2 parameters
def partial2(fn):
def inner1(arg2):
def inner2(arg1):
return fn(arg1, arg2)
return inner2
return inner1
# a simple function of 2 parameters
def hello(firstname, lastname):
print "hello %s %s" % (firstname, lastname)
hello = partial2(hello)
# perform partial application of hello, binding only the first argument.
# this prints nothing and returns a curried function of 1 parameter
hello_with_firstname = hello('Mickey')
# bind the final argument, which completes the function application
# and finally prints "hello Mickey Mouse"
hello_with_firstname('Mouse')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment