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.
Again please note that in the following code
R stands for
Ramda and not
for programming language R.
if (x) return x; return ;
Here is same code using Ramda.js.
R.ifElse( R.isNil, () => , R.identity );
Here we will use identity as the return value in the default case.
R.cond([ [R.equals(0), R.always("0")], [R.equals(10), R.always("10")], [R.T, R.identity] ]);
Get the unique items from the list.
Count occurrences of items in the list.
Begin value from zero all the way to n-1.