Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where and why is identity function useful?

I understand why function composition is important. It allows building large and complex functions from small and simple ones.

val f: A => B = ...
val g: B => C = ...

val h = f andThen g; // compose f and g

This composition conforms to identity and associativity laws.

Associativity is useful because it allows grouping f1 andThen f2 andThen f3 andThen f4 ... in any order. Now I wonder why identity is useful.

def f[T](t:T) = t   // identity function
val g: A => B = ... // just any function
g andThen f[B] == f[A] andThen g 

So, my question is where and why this identity useful.

like image 330
Michael Avatar asked Feb 01 '14 22:02

Michael


People also ask

Why is identity function useful?

On a more theoretical side (and since I can guess where this question has arisen from), the one fundamental use of the identity functions is that they allow you to define the notion of isomorphisms, which often turn out to be a much more useful definition of "sameness" than usual equality (read this).

What is an identity function in math?

The identity function is the function which assigns every real number to the same real number. . It is identical to the identity map.

What is an identity function programming?

An identity function, in the context of the code in your question, is simply a function that returns the same argument passed to it : In math you can denote it as: f(x) = x.

What is a identity function Python?

Overview. The identity() function from numpy library is used to return the identity matrix. Note: An identity matrix is the type of matrix in which all the elements on the main diagonal are ones (1) and all other elements are zeros (0).


1 Answers

The identity function is, well, the identity element for function composition, like 1 for multiplication or 0 for addition. We need it like we need 1 or 0. For instance, suppose we are writing a (higher-order) function that takes a list of functions and returns their composition: it is then natural to return the identity function for an empty list. This is just like we return 0 for an empty list if we are writing a function that takes a list of integers and returns their sum.

like image 175
FPstudent Avatar answered Sep 18 '22 20:09

FPstudent