Explore Functional Programming more with

Lambdas & Closures

Seducing slowly away from Imperative to Functional Programming | Day 10 of #100DaysOfCode

Dhananjay Trivedi
3 min readSep 27, 2019

Lambdas and Closure are not something out of the ordinary, they are just way of writing code which is far less painful to write as it used to be in the imperative style of programming. That’s one of the beauties of Functional Programming where we can get a lot more done in a lot less code. How cool is that!

I am coming from a Java background so the code example would be in Java/Kotlin so bear with me. Some implementation might differ for people in different languages so just a heads-up! But the emotions and intentions will be the same.

Lambdas

Lambda is just an anonymous function which has no name. Remember in Java we have anonymous inner classes (sounds spooky). They were used when we were trying to implement an interface which has one abstract function. Now, here is the thing to use that interface, we have two options.

  1. Create a class from scratch which implements that interface and then finally initialize our new class and use its object which overrides the interface’s method.
  2. Use anonymous class where we directly initialize interface followed by a class definition where we have definition for implementing the method. This can be done in-line without having to create a class from scratch. Example:

button.addActionListener() required an object of a class which implements ActionListener() interface. The interface has an abstract method which all classes implementing this interface must override. Here we do exactly that, everything in-line. We create a class which has the same name as that of Interface followed by {} where we define the implementation logic for actionPerformed().

This was the boiler-plate code we all had to write to actually write the logic we actually want to use and care about!

Lambdas help you do that exactly! Helps you focus on your logic without having to write the boiler-plate code. So our old anonymous inner classes can now be refactored as shown below.

  • Lambdas work for interfaces which have only one abstract function, and hence whatever you define inside the {} will be taken as the implementation for that function.
  • You can leave them blank if you want.
  • For a single statement, you can avoid even the enclosing {} but to wrap multiple statements you require {}

Closure

‘Closure’ comes from the wonderlands of Functional Programming. Closure are stateful functions which means they have some state of the program holding inside them. In simpler words, they are functions that have references to variables which are in the outer scope (beyond that function).

Let’s take this Kotlin code as an example:

Here our function getDevDeejay() returns us list of students whose last name is “devDeejay”. BTW, We are using a filter function here, the filter function itself takes lastName as a parameter which is actually a field of Student class. Hence, our filter here closes-over the state of the class, hence the filter here is a Closure.

Since they are closing over the variables, they have the power to mutate them. Here is a quick example:

Here you can see we are mutating containsNegative boolean variable.

That’s a great thing about closures, we are not creating some new variables to iterate over our list of objects, everything is encapsulated within the function and everything is pretty neat to read and maintain.

Read more about some more functions from Functional Programming which I recently wrote about here.

That’s all for Day 10 folks! Have a good weekend! Keep learning something new!

Hey Reader,

10 Days 10 Stories! It’s going really good, Thanks for asking! Teaching is defintely one of the best ways to learn something and I have been learning, re-learning things from a whole new perspective which I didnt before so this really feels great.

The number of views and followers is steadily increasing for #100DaysOfCode, ‘devDeejay’ name is getting out there as well! Just want to take a moment and thank you guys for taking out time to read the stories and even clap for them! This really motivates me to put in my very best and explore my fullest writing potential to bring in most value in the upcoming 90 Stories!

You are awesome!


devDeejay

--

--

Dhananjay Trivedi
Dhananjay Trivedi

Written by Dhananjay Trivedi

Android | Flutter | App Developer | Product Management | Prompt Engineer who loves writing and sharing knowledge

No responses yet