Python function currying

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Python function currying

1 currying(Currying)

The function has a plurality of parameters, we hope to fix the values of several parameters.

from functools import partial
def foo(a,b,c):
    return a+b+c

foo2 = partial(foo, b=2)
foo2(a=1, c=3)

It seems to provide a default parameter values were similar. But the default value can only be fixed

As a single value, and currying can through generalization of many functions, each function

Fixed values for different application scenarios, see :

from functools import partial
bin2dec = partial(int, base=2)
hex2dec = partial(int, base=16)

Definition of the original int method: int (x[, base]), the default for the base parameter is 10

After currying, can call in the following manner:

int('15') #=>15  using default base 10
bin2dec('01011') #=>11
hex2dec('67') #=>103

2 anti currying(Uncurrying)

As the name suggests, the reverse process of curry.

A plurality of containing only a single parameter function is simulated as a multi parameter function.

def foo(a):
    def bar(b):
        return a+b
    return bar

You can be called like this: foo (1) (4) or (foo (1)) (4), can get the correct results in 5.

Some of the functional programming language, is the use of anti currying to realize multi parameter function.

As far as can be used in any place, I don't know, just as interested in.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Kennedy at November 13, 2013 - 12:08 AM