Kotlin Lambda & Higher Order Functions

In this article, We will learn how to create lambda function in kotlin programming language and how to pass higher order function in other functions in kotlin programming language.

Lambda Function has no name. We can pass Higher Order Function as parameter in other functions.

Lambda Function

Lambda Functions is a function without a name. Lambda Function is defined by curly braces {}. We can define our own lambda function. The main feature of lambda function is that it reduces boiler plate code.

The Function Body is written after -> operator which is written after variable. If the return type of lambda function is not unit then last statement is assigned as return value.

{ variable -> body_of_function}
 

We do not require variable always. We can also pass variable as a parameter directly to the function.

For Example:

fun main(args: Array<String>) {
     val website = {      println(“CodingWithDhrumil”)}
     // call function
     website()
}
 

Type Interference helps compiler to check type of lambda expression in kotlin.

val sum = {a: Int , b: Int -> a * b}
 

Kotlin compiler evaluate the above example to be a function that takes two parameters of type Int and returns Int value.

We must explicitly our lambda expression type. If the function returns no value then we can use type Unit.

val value: (Int) -> Int = { s -> s }
 

We can also use lambdas as class extension.

val value: String.(Int) -> String = { this + it }
fun main(args: Array<String>) {
     val result = “CR”.value(7)
     print(result)
}
 

In the above example, this keyword is used for the string. Also function accepts only one parameter. So we can call parameter by using keyword it. it is shorthand of a single argument in lambda expression.

val square = { it * it}
 
 

Higher Order Function

Higher Order Function can accept function as a parameter. We can also pass it as argument to other functions. We can pass anonymous function or lambda function in higher order functions. Mostly lambda functions are passed to higher order function.

For Example:

fun name(website: () -> Unit) {
     website()
}

fun main(args: Array<String>) {
     name({ println(“CodingWithDhrumil”) })
}

 

We can pass both lambda functions in higher order functions: lambda functions with unit return type and lambda functions with any value return type.

For Example: lambda functions with unit return type

var lambdaFunc = { println(“CodingWithDhrumil”) }

fun highfunc( lmbFuncParam: () -> Unit ) {
     lmbFuncParam()
}

fun main(args: Array<String>) {
     //call higher order function
     highfunc(lambdaFunc)
}

 

In above example, () indicates function has no argument and Unit indicates that function does not return any value.

For Example: lambda functions with any value return type

var lambdaFunc = {a: Int , b: Int -> a + b }

fun highfunc( lmbFuncParam: (Int, Int) -> Int) {
     var result = lmbFuncParam(5,6)
}

fun main(args: Array<String>) {
     highfunc(lambdaFunc)
}

Leave a Reply