Created
June 11, 2009 20:15
-
-
Save vlazzle/128202 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
################################################# | |
# 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