Functional Basics: Identity Function
The identity function is an unassuming little function with a signature of 'a -> 'a
. It returns the value of the argument it's given. At first glance it can seem rather pointless but it serves a purpose in both practical and theoretical contexts.
The identity function is probably the first function you learn in lambda calculus, the basis of all functional programming. Thus, theory is critical. Functional programming values simplicity and purity. Each function should be small and pure - meaning it does not cause side effects - and should be expressed in a way that allows the invoked function to represent a value itself.
All of these foundational characteristics of functional programming are found in the humble identity function.
Simplicity - The function takes a single parameter and returns a single value.
Purity - Other than doing exactly what is required to return the value, no other operations are performed.
Expression - The function, given an argument, can represent the value that is returned. Eg let theNumberFour = identity(4)
equates to let theNumberFour = 4
.
Functional programming also promotes function composition—building complex operations from simpler functions. Here, the identity function acts as the neutral element, analogous to multiplying by one or adding zero. Composing any function with the identity function leaves the original function unchanged.
Say you have a function increment
defined as let increment x = x + 1
. If you were to compose this function with identity
, since identity returns it's argument as the value, you'd end up with increment
. Thus if we define let func = increment >> identity
(the >>
operator is the compose operator, similar to Lodash.js or Rambda.js compose
), and then execute func(1)
we end up with 2
.
Finally, from a practical perspective, the identity function is useful in handling monoids - structures featuring an associative operation and an identity element. A quick example of a monoid is the set of integers with addition where zero is the identity. In 1 + 0 = 1
, zero is the identity element. In "hello" + "" = "hello"
the empty string is the identity element. Identity elements are essential, not just in theory, but in scenarios in programming where we need to meet function signatures, handle the absence of values, etc. In functional programming, the identity function mirrors this concept, an unchanged input equating to zero addition.
As a student of functional programming, it is important to embrace the fundamentals first. Even practicing the fundamentals in a thoroughly OOP codebase can be enlightening in your journey to FP. This starts with replacing imperative logic within methods with expressions, single argument functions, and understanding functional purity. The most significant first step is to consider how you can achieve something in a series of single-argument functions (or methods) which return a value. Understanding and appreciating the identity function is a great way to take this first step.