We have already explored the introduction to FP in my previous blog.
Once you get into FP, you’ll quickly start hearing the terms lambda and lambda calculus.
Lambda is basically just a symbol represented as λ that Alonzo Church chose when he first defined the concept of a function. In modern functional programming, lambda means “anonymous function“.
Calculus is means a formal system in mathematical logic for expressing computation. It is basically the mathematical study of continuous change.
“Lambda calculus provides a theoretical framework for describing functions and their evaluation”. It forms the basis of almost all functional programming languages today.
Now, after getting familiarity with these terminologies, let’s see some examples of lambda with respect to its properties.
Characteristics of Lambda:
- Can be assigned to a variable.
- Anonymous functions.
Lambda is nothing but an anonymous function. In the above two examples, value even and odd can be used as a function, i.e., even(4) will return true and odd(4) will return false.
- Can be passed to functions.
Now, we can call this method by passing a lambda expression, for example,
Observe these statements closely, we can easily see the benefits of doing it. Yes, we can call sum() by multiple lambda expression having different implementations.
- Can be returned by functions.
You can define a function that returns an anonymous function, assign that to a new function, and then call that new function. Let’s see an example:
Now again see these statements closely, we see first, the method say() returns an anonymous function. We are assigning it to a value greet. Now we can easily greet anyone by just passing their name.
- More readable, concise and maintainable code.
The code is very much readable and concise, as you can see in the above two examples. This helps in better maintenance of code.
There are quite a few reasons to explain the need for lambda :
- Will I use the function anywhere else:
There are many scenarios where we just use defined function at the single place. This may seem okay at first, but as the application grows, these methods also grow at a very high rate. So the use of lambda expression can reduce the so-called boilerplate code in these cases.
- Less Byte Code generated:
Lambda generates less byte-code as compared to the functions. And considering the number of functions which are replaced by these lambdas in a big application, a much compact size of byte-code is being transferred over the network (which helps in achieving good network speed) and being persisted into the storage (which saves storage space).
- Fast and Efficient:
They are fast as they follow the in-place replacement algorithm.
For a simple function that is used only in one place, a lambda is quicker and easier, and more expressive, than the alternative of declaring a whole first.
Limitations/Shortcomings of Lambda?
Great pros often come with some limitations :
- More concise code often leads to more complex code debugging and longer synthetic call stacks.
- Developers with OOPs background often find lambda expression confusing to start with, but once comfortable, lambdas are fun.
So Lambda expressions are nothing but anonymous functions and are considered to be the backbone of Functional Programming paradigm.
References: Functional Programming Simplified by Alvin Alexander