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.
Here is JavaScript code.
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.
R.uniqBy(R.identity, [1, 1, 2]);
Count occurrences of items in the list.
R.countBy(R.identity, ["a", "a", "b", "c", "c", "c"]);
Begin value from zero all the way to n-1.
R.times(R.identity, 5);
Filter truthy values.
R.filter(R.identity, [
{ a: 1 },
false,
{ b: 2 },
true,
"",
undefined,
null,
0,
{},
1,
]);

Related blogs
- 01Benchmarking Crunchy Data for latencyOctober 15, 2024 · Vishnu M
- 02Improving NeetoRecord reach with Twitter player cardsAugust 16, 2024 · Bonnie Simon
- 03Honeybadger frontend integration in Neeto appsDecember 12, 2023 · Calvin Chiramal
- 04Deep Dive into Redis Data TypesNovember 14, 2023 · Sreeram Venkitesh