lesson 38 – Lambda expressions in Kotlin language

A lambda expression is when we send an anonymous function directly to a higher-order function.

It is more common to send a lambda expression instead of sending the reference of a function as we saw in the previous concept.

Problem 1

Define a higher order function called operate. They arrive as a parameter two integers and a function. In the function block call the function that arrives as a parameter and send the first two parameters.

From the main function call to operate and send different lambda expressions that allow adding, subtracting and raising the first value to the second.

Within the lambda expression, we can implement a more complex algorithm as it is in this case where we raise the parameter x to the indicated exponent with y.

It is important to note that the higher order function receives as a parameter a function with two parameters of type Int and returns an Int:

Fn: (Int, Int) -> Int

The lambda expression must pass a function with the same structure, i.e two integer parameters and return an integer:

{X, y ->
Var value = 1
For (i in 1..y)
Value = value * x
value
}

We can identify the two parameters x and y, and the value that returns is the indicated data after the for.

A last change that we will implement to our program is that when a lambda expression is the last parameter of a function we can indicate the expression lambda after the parentheses so that the code of our program is more readable:

To print the multiple values of 2 our lambda expression receives as parameter an Int called xy and the algorithm that verifies whether it is even or not consists of verifying if the remainder of dividing by 2 is zero:

It is important to understand that our lambda expression does not have a repetitive structure, but from the print function, it will call this anonymous function as many times as the elements have the array.

The algorithm of the function varies if we want to identify if the number is a multiple of 3 or 5:

In very simple form if we want to print the whole array we return for each element that analyzes our expression lambda the value true:

Println ("Print all values")
PrintIf (array1) {x -> true}

This is a problem where we can already notice the potentialities of the higher order functions and the lambda expressions that we pass to them.

Acotaciones

We said that one of the principles of Kotlin is to write concise code, for this when we have a lambda expression whose function receives a single parameter we can obviate it, including the sign ->
Then by convention that single parameter we can refer to it with the word ” Item”

Then our definitive program should be written with the following syntax:

Our higher order function is called filtering and receives a String and a function:

Fun filter (String: String, fn: (Char) -> Boolean): String

The function that receives has a parameter of type Char and returns a Boolean.

The algorithm of the filter function consists of traversing the String that arrives as a parameter and calling the function fn that is the one that will inform if the analyzed character must be part of the final String to return.

By means of an object of class StringBuilder we store the characters to return, before returning we extract as String the contents of StringBuilder: