Menu

Currying in Javascript and C#, to Serverless Currying?

3rd December 2018 - Uncategorised

Who is Curry

Haskell Brooks Curry was an American mathematician best known for his work in combinatory logic.
He has three programming languages named after him, Haskell, Brooks and Currying, as well as the Currying concept in computer science.

What is Currying

While I do love a nice curried trout, currying has nothing to do with food, and more to do with calculus. It refers to a method of computation where a function returns a function.

We use this in computer science to create a stateless chain of functional parts which, in total, describe a whole application.

e.g.
Suppose I have an function, to create a dog.


function MyDog(breed,colour,size){
  return "My "+size+" "+colour+" dog is a "+ breed
}

called as MyDog(“corgi”,”red”,”large”)

This can be curried into

function MyDog(breed){
  function(colour){
    function(size){
      return¬†"My "+size+" "+colour+" dog is a "+ breed
    }
  }
}

and is called as MyDog(“corgi”)(“red”)(“large”)

Ultimately, it allows far more composition in a class and introduces a functional way of thinking about class creation.

Currying in FSharp/OCAML

As Fsharp adheres to the OCAML language specifications, currying is a core part of the way the language is written, so it’s often stated that with FSharp you “get currying for free”.


let MyDog breed =
  let subFunction colour =
    let subFunction size =
      printfn "My %s %s dog is a %s" size colour breed
    subFunction
  subFunction

MyDog("corgi")("red")("large")

Source

Currying in Javascript

using ECMAScript 2016, and lambda notation, currying is trivial in javascript.

let MyDog =
  colour =>;
    breed =>;
      size =>; "My "+size+ " "+ colour + " Dog is a " + breed

console.log(MyDog('red')('corgi')('large'))

Source

Currying in CSharp

With CSharp, functional aspects of it’s language have been growing in the last few years, spearheaded by VB.Net and its historical functional foundations, as well as a general movement towards functional structures linking local applications and cloud services.

It’s now considered a primary focus within the ongoing development of the language to allow functional paradigms to drive application architecture and development

Note: The example is in C# 4.7.2

namespace FuncyConsoleApp1
{
  using System;

  class MainClass
  {
    public static void Main(string[] args)
    {
      Func<T1, Func<T2, Func<T3, TResult>>> Curry<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> function)
      {
        return a => b => c => function(a, b, c);
      }
    
      string MyDog(string a, string b, string c) => $"My {c} {b} Dog is a {a}";
      Console.WriteLine(MyDog("red","corgi","large"));
      
      var curried = Curry<string,string,string,string>(MyDog);
      Console.WriteLine(curried("red")("corgi")("large"));
      Console.ReadLine();
    }
  }
}

See also: Jon Skeet, Dixin

Calling a cloud function

Coming soon…

Calling a cloud function from a cloud function

AWS Step functions | Azure Durable Functions

Serverless Cloud Currying?

Coming soon…

Skynet!?

Hopefully not Coming soon…

Leave a Reply