If you are learning functional programming then you can’t go far without running into “identity function”.

An identity function is a very basic function that

  • takes one argument
  • returns the argument
f(x) = x;

This seems like the most useless function in the world. We never needed any function like this while building any application. Then what’s the big deal about this identity function.

In this blog we will see how this identity concept is used in the real world.

For the implementation we will be using Ramda.js. We previously wrote about how we, at BigBinary, write JavaScript code using Ramda.js.

Again please note that in the following code R stands for Ramda and not for programming language R.

Example 1

Here is JavaScript code.

if (x) return x;
return [];

Here is same code using Ramda.js.

  () => [],

try it

Example 2

Here we will use identity as the return value in the default case.

  [R.equals(0), R.always("0")],
  [R.equals(10), R.always("10")],
  [R.T, R.identity]

try it

Example 3

Get the unique items from the list.

R.uniqBy(R.identity, [1,1,2])

try it

Example 4

Count occurrences of items in the list.

R.countBy(R.identity, ["a","a","b","c","c","c"]);

try it

Example 5

Begin value from zero all the way to n-1.

R.times(R.identity, 5)

try it