Currying and Partial Applications in F#, Javascript and C#

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#.


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

// 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

From the method sig­na­tures you can see how the result of the two approach­es dif­fers:


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

partial 1 // returns 16

clear­ly, this is equiv­a­lent to call­ing

f 1 5 10 // returns 16

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

With the cur­ry func­tion, how­ev­er, it’s slight­ly more inter­est­ing:

((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


Despite being con­sid­ered an object-ori­ent­ed lan­guage, Javascript shares more than a hand­ful of sim­i­lar­i­ties with func­tion lan­guages, take its dynam­ic, loose type sys­tem, or the abil­i­ty 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


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

// 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

Parting 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­less­ly – clo­sure. If you’re not famil­iar with the con­cept of clo­sure, it’s def­i­nite­ly 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 fea­ture.


Wikipedia page on Cur­ry­ing

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

Angus Croll – Cur­ry: cook­ing up tasti­er func­tions

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

Olivi­er Steel – Func­tion­al Javascript

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

Lamb­da the Ulti­mate – Cur­ry­ing != Gen­er­al­ized Par­tial Appli­ca­tion?!

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

Did it with .Net – The Art of Cur­ry­ing