Functional Programming — Curry

Image for post
Image for post
Photo by Who’s Denilo ? on Unsplash

Not long ago, I came across functional programming by chance and really fall for it. Even I’m not intended to go too deep into (so it won’t distract me too much from python learning), I decided to give it a go and look at some basic concepts.

So we often heard of Functional Programming as some kind of doctrinal thinking like one input, one output. But what exactly is it?

In a nutshell, Functional Programming has three pillars:

  • Total: have a corresponding output for every input
  • Deterministic: the same output given the same input
  • No Observable Side-Effects: not even a log!

For example:

An there a few attributes of it:

Don’t worry if you don’t get some of the points yet, through the following series of blogs, you will understand as we go.


The first and foremost concept we will look at is curry.

Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It will always return a new function that takes a single argument one at a time until it running out the arguments. All the arguments are accessible via closure and executed when the final function is returned.

Let’t see an example.

The above function can be written as:

In JavaScript, as some functional programming concepts are not built in, there is this really amazing library “ramda” to help us implement most of the concepts easily.

Another example:

Let’s make a function that takes any function and returns a curried version of the function:

So we receive a function and a list of arguments in rest parameter. Next, we return a function that also collects the rest of the parameters as …_args. This function invokes the original function fn passing in ...args and ..._args through the use of the spread operator as parameters.

There’s a related concept call Partial Application.

What is a partial application?

A partial application as its name suggested, is a function where some of its arguments are used(fixed).

Partial applications can take as many or as few arguments while Curried functions only takes one argument at a time.

Partial application transforms a function into another function with smaller arity (the number of arguments) comparing to its original arguments number. In the above multiply example we can rewrite it to a partial application:

Apart from partial application, another key concept is point-free.

Point-free style is a style of programming where function definitions do not mention the function’s arguments.

How can this be possible? Well, you can call a function that returns a function.

To make a function that will add 1 to any number:

We can also have add 2, add 3 as we want. We can see that the returned function is just a more specialized of the more general add() function.

Does this form remind you of anything?

Yes all curried functions are higher-order function which allows you to fine tune the original function.

With curry functions we can have a containers of reusable code that we can fixed some certain arguments if we know for certain, while having a list of undecided arguments that we can generate on the fly. So we are able to only need the undecided arguments while keep it DRY.

Finally some examples using ramda.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store