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

Yan Cui

I help clients go faster for less using serverless technologies.

In my last post I explained the difference between the techniques of Currying and Partial Application, following on where there let me show you how you might apply these two techniques in F#, Javascript and C#.

F#

Starting with F#, being a functional language it makes currying and partial application dead easy (considering that they are primarily function programming concepts). Take for example, a simple function 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 signatures you can see how the result of the two approaches differs:

image

To use the partially applied function partial:

partial 1 // returns 16

clearly, this is equivalent to calling

f 1 5 10 // returns 16

seeing as the partial function has fixed a2 and a3 to 5 and 10 respectively.

With the curry function, however, it’s slightly more interesting:

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

this way of invoking the chained function in turn is in fact the same as:

curry 1 5 10 // returns 16

Javascript

Despite being considered an object-oriented language, Javascript shares more than a handful of similarities with function languages, take its dynamic, loose type system, or the ability to treat function as objects, for instance. It’s therefore of little surprise to see that you can implement currying in Javascript with ease.

Here’s the example 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 possible! Here’s a simple example 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

Parting thoughts…

There is one important language feature which all three languages support, which is what enables us to apply both currying and partial application so effortlessly – closure. If you’re not familiar with the concept of closure, it’s definitely worth spending a bit of time familiarise yourself with it as it’s a very powerful (albeit often overlooked) language feature.

References:

Wikipedia page on Currying

John Resig’s post on currying in JavaScript

Angus Croll – Curry: cooking up tastier functions

Angus Croll – partial: curry’s flashy cousin

Olivier Steel – Functional Javascript

SO question – what is the difference between currying and partial application

Lambda the Ultimate – Currying != Generalized Partial Application?!

Good blog posts which explains the differences between Currying and Partial Application

Did it with .Net – The Art of Currying


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


2 thoughts on “Currying and Partial Applications in F#, Javascript and C#”

  1. Pingback: Tweets that mention Currying and Partial Applications in F#, Javascript and C# | theburningmonk.com -- Topsy.com

Leave a Comment

Your email address will not be published. Required fields are marked *