vovik (owner)

Forks

Revisions

gist: 128202 Download_button fork
public
Public Clone URL: git://gist.github.com/128202.git
Embed All Files: show embed
curry.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#################################################
# 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')