I came across this subject when I was reading through PrototypeJS's docs: its Identity Function. I did some further searching&reading on it and I think I understand its mathematical basis (e.g. multiplication by 1 is an identity function (or did I misinterpret this?)), but not why you would write a JS(or PHP or C or whatever)-function that basically takes X as a parameter and then just does something like return X
.
Is there a deeper insight connected to this? Why does Prototype supply this function? What can I use it for?
Thanks :)
The identity function is the function which assigns every real number to the same real number. . It is identical to the identity map.
The function f is called the identity function if each element of set A has an image on itself i.e. f (a) = a ∀ a ∈ A. It is denoted by I. Example: Consider, A = {1, 2, 3, 4, 5} and f: A → A such that. f = {(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)}.
Using the Identity function makes the library code slightly easier to read. Take the Enumerable#any method:
any: function(iterator, context) {
iterator = iterator || Prototype.K;
var result = false;
this.each(function(value, index) {
if (result = !!iterator.call(context, value, index))
throw $break;
});
return result;
},
It allows you to check if any of the elements of an array are true in a boolean context. Like so:
$A([true, false, true]).any() == true
but it also allows you to process each of the elements before checking for true:
$A([1,2,3,4]).any(function(e) { return e > 2; }) == true
Now without the identity function you would have to write two versions of any function, one if you pre process and one if you dont.
any_no_process: function(iterator, context) {
var result = false;
this.each(function(value, index) {
if (value)
throw $break;
});
return result;
},
any_process: function(iterator, context) {
return this.map(iterator).any();
},
I do not know about that library, but normally, you optimize formuals or code or whatever by factoring common part out, like if (add) (a + b) + x else a + b
should be rewritten into a + b + (add ? x : 0)
. You are tempted to do the same with
if (!initialized) initialize(callback_with_very_long_name) else callback_with_very_long_name
Looks pretty similar. You can easily factor out a common factor or term but how do we factor out a function application? If you understand mathematiscs or Hascel, you should see that
a ? x + v : v
looks very much like
a ? f value : value
You add x in one case but not in the other. You apply function in one case but not in the other. You optimize the former into (a ? x : 0) + v
because 0 is additive identity (it does not change anything when added to it) and v
is a common factor here, which comes always, regardless of application of x
. In case of function application (or not application), the callback is the common factor. We want to factor it out. What is the identity function that we should apply to it so that nothing changes? Identity function!
(a ? f : identity) value
is what we are looking for. Our original example looks like the following then
(initialized ? identity : initialize) (callback_with_very_long_name)
Please note that it fits into one row of page now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With