In my last post I explained the dif­fer­ence between the tech­niques of Cur­ry­ing and Par­tial Appli­ca­tion, fol­low­ing on where there let me show you how you might apply these two tech­niques in F#, Javascript and C#.

F#

Start­ing with F#, being a func­tional lan­guage it makes cur­ry­ing and par­tial appli­ca­tion dead easy (con­sid­er­ing that they are pri­mar­ily func­tion pro­gram­ming con­cepts). Take for exam­ple, a sim­ple func­tion f which takes 3 parameters:

// base function f
let f a1 a2 a3 = a1 + a2 + a3

// partial function fixes two of the parameters
let partial a1 = f a1 5 10

// currying creates a china of functions each taking one parameter
let curry a1 =
    // create the next function in the chain which takes another parameter
    // and returns another function
    let curry' a2 =
        // the inner most function gives you the full application
        // of the original function
        let curry'' a3 = f a1 a2 a3
        curry''
    curry'

From the method sig­na­tures you can see how the result of the two approaches differs:

image

To use the par­tially applied func­tion par­tial:

partial 1 // returns 16

clearly, this is equiv­a­lent to calling

f 1 5 10 // returns 16

see­ing as the par­tial func­tion has fixed a2 and a3 to 5 and 10 respectively.

With the curry func­tion, how­ever, it’s slightly more interesting:

((curry 1) 5) 10 // returns 16

this way of invok­ing the chained func­tion in turn is in fact the same as:

curry 1 5 10 // returns 16

Javascript

Despite being con­sid­ered an object-oriented lan­guage, Javascript shares more than a hand­ful of sim­i­lar­i­ties with func­tion lan­guages, take its dynamic, loose type sys­tem, or the abil­ity to treat func­tion as objects, for instance. It’s there­fore of lit­tle sur­prise to see that you can imple­ment cur­ry­ing in Javascript with ease.

Here’s the exam­ple I used in the last post:

// original function that takes three parameters a1, a2 and a3
function f(a1, a2, a3) {
    return a1 + a2 + a3;
}

// curried function which takes one parameter at a time
function curry(a1) {
    // each stage takes another parameter and get you closer to the
    // full application of f
    return function (a2) {
        // but only with the inner most function do you actually get
        // the return value you wanted
        return function (a3) {
            return f(a1, a2, a3);
        };
    };
}

// partial applied function which takes one parameter and fixes
// the other 2
function partial(a1) {
    // a partially applied function of the original function f can
    // get you the full application straight away
    return f(a1, 5, 10);
}

curry(1)(5)(10); // returns 16
partial(1); // returns 16

C#

Yes, it’s pos­si­ble! Here’s a sim­ple exam­ple of how using the Func delegate:

// base function f
Func<int, int, int, int> f = ( a1, a2, a3 ) => a1 + a2 + a3;

// note that partial is a reserved keyword in C#
Func<int, int> partialf = a1 => f(a1, 5, 10);

Func<int, Func<int, Func<int, int>>> curryf = a1 => a2 => a3 => f(a1, a2, a3);

f(1, 5, 10);  // returns 16
partialf(1);  // returns 16
curryf(1)(5)(10);  // returns 16

Part­ing thoughts…

There is one impor­tant lan­guage fea­ture which all three lan­guages sup­port, which is what enables us to apply both cur­ry­ing and par­tial appli­ca­tion so effort­lessly – clo­sure. If you’re not famil­iar with the con­cept of clo­sure, it’s def­i­nitely worth spend­ing a bit of time famil­iarise your­self with it as it’s a very pow­er­ful (albeit often over­looked) lan­guage feature.

Ref­er­ences:

Wikipedia page on Currying

John Resig’s post on cur­ry­ing in JavaScript

Angus Croll – Curry: cook­ing up tastier functions

Angus Croll – par­tial: curry’s flashy cousin

Olivier Steel – Func­tional Javascript

SO ques­tion – what is the dif­fer­ence between cur­ry­ing and par­tial application

Lambda the Ulti­mate – Cur­ry­ing != Gen­er­al­ized Par­tial Application?!

Good blog posts which explains the dif­fer­ences between Cur­ry­ing and Par­tial Application

Did it with .Net – The Art of Currying

Share

2 Responses to “Currying and Partial Applications in F#, Javascript and C#”

  1. […] This post was men­tioned on Twit­ter by Daniel Mohl. Daniel Mohl said: “Cur­ry­ing and Par­tial Appli­ca­tions in F#, Javascript and C#” http://bit.ly/fUP2Z2 #fsharp #javascript #csharp […]

  2. Gary Yourman says:

    I found some pretty cool curry exam­ples on the offi­cial jPaq site: http://jpaq.org/examples/7 . These exam­ples show how you can use just one line of code to accom­plish a lot with pseudo-functional programming.

Leave a Reply