As the title of this article described today’s piece cover using lambda, map, and filter in Kotlin. We’ll be covering the syntax and usage of each function and walk through some example to familiarize yourself with using them.

So, let’s get started with the lambda definition.

What is Lambda Function

Kotlin allows you to create a one-time anonymous function, function having a no names using a facility called lambda function. Lambda functions are small usually not more than one line.

Before showing you the basic syntax of how to write a lambda expression, I want to show you the simple function in Kotlin.

The above function name is multiply, it expects two arguments x and y with type Int and returns their multiplication.

Now let’s see how we can convert the above function in lambda function.

Basic Syntax of Lambda Function

The full syntax of a kotlin lambda expression is as follows:

In the above code at the left of -> is the parameter type, enclosed in parentheses (Int, Int). The result type indicating on the right side of the arrow. The definition of the function comes after = sign. It’s enclosed between braces and takes the form of the lambda expression.

The lambda expression returns the multiplication of two numbers as an Int type number and assigns to the multiply variable.

In last we simply call the multiply expression as we’re calling any basic function.

You can also omit the type of lambda expression like this:

Protip:

If you prefer shorter type name, you can create an alias at project-level:

To make our typealias more reusable you can change it into a more generic style.

Filter Function

As the name suggests, the filter function filters all those items that pass the predicate (some sort of if condition) test. Before showing you the magic of filter function I wanna show you how we apply the filter function manually without using the functional-programming approach.

Now let’s say I have the list of Person and I want all those persons whose age greater than or equals to 25.

You see at the above we’re creating everything on our own instead of using the _Collection class utility function. Now let’s see the magic of filter, I’m pretty sure you’ll love the most and use in your day-to-day development.

Basic Syntax of Filter Function

Note: The filter function is only applicable to collection classes.

As you can see above the agePredicate is a lambda function accepting Person as input and returns true if the person.age is greater than or equal to 25 else false.

The filter accepts the lambda as input, applies the predicate condition on every item inside the list, and returns a new list containing all the items that pass the predicate condition.

You can also pass the predicate directly inside the filter function like below.

Map Function

Think of map function as some kind of transformation, applies a function (a lambda) to all the items and returns a list of items modified by the function. Let’s see how we can use the map function on a Person collection.

In the above example, map executes the lambda function for each person inside the collection and returns a list of string.

Let’s see the above example by using function reference (pass it to another function) instead of directly passing the lambda.

Alright, guys, if you enjoyed this article kindly spread the word and don’t forget to hit the ♥️ button. To get updates for my new stories, subscribe to my website at home page.

Thank you for being here and keep reading…

More Resource

10 Kotlin List Tips & Tricks

Author

Write A Comment