Kotlin Lambda Expressions, Higher-Order Functions

Filed Under: Kotlin

In this tutorial, we’ll be looking into kotlin higher-order functions and lambda expressions in detail. At the same time, we’ll explore function references, anonymous functions, and closures too.

Kotlin Higher-Order Functions

Kotlin supports functional programming. High order functions have the ability to pass a function as an argument or use it as the return value. Let’s see this through examples.

Function Types and References

Functions in Kotlin are types.
()->String is a type that takes no parameters and returns a string.
(String)->String is a type that takes a string argument and returns a string argument.

Kotlin Lambda Expressions

kotlin lambda expression
Lambda Expressions are function literals that help us in writing a function in a short way. They provide us a compact way of writing code. Let’s define a lambda expression to see how functions act as types.


fun main(args: Array<String>) {

    var lambdaFunction :(String)->Unit  = {s:String -> println(s)}
    lambdaFunction("Kotlin Lambda Functions")

    //or
    lambdaFunction =  {println(it)}
    lambdaFunction("Kotlin Lambda Functions Concise")

    val noArgFunction : () -> Unit ={ println("Another function")}
    noArgFunction()

}

//Following is printed on the console.
//Kotlin Lambda Functions
//Kotlin Lambda Functions Concise
//Another function

lambdaFunction property has a function as its type. The right-hand side is where the function is declared.

it is used to access parameter values in the body of the lambda expression.

We can pass a function as a parameter inside another function too using references as shown below.

Such functions are known as High Order Functions.


fun main(args: Array<String>) {

    var printFunction: (String) -> Unit = { println(it) }
    functionReferencesExample("JournalDev.com", printFunction)

}

fun functionReferencesExample(str: String, expression: (String) -> Unit) {
    print("Welcome To Kotlin Series @")
    expression(str)
}

To pass a function as an argument inside another function we need to use the notation ::. Following code snippet demonstrates an example on the same.


fun main(args: Array<String>) {
    functionReferencesExample("JournalDev.com", ::printFunction)
}

fun functionReferencesExample(str: String, expression: (String) -> Unit) {
    print("Welcome To Kotlin Series @")
    expression(str)
}

fun printFunction(str: String) {
    println(str)
}

Note: We’ll look at High order functions and lambda expressions in detail in a later tutorial.

Lambda Expressions inside Higher Order Functions

A Lambda expression can be passed inside a higher order function parameter as shown below.


fun main(args: Array<String>) {
    printMe({ println("Lambda Inside a function")}) //prints Lambda Inside a function
}

fun printMe(string1: () -> Unit) {
    string1()
}

The lambda expression as a parameter runs as a function inside another function.

Another example demonstrates using lambda expressions inside print function.


fun main(args: Array<String>) {
println("Let's invoke a lambda function ${returnMe { "return Me " + "function"  }} here")
}

fun returnMe(string: ()->String) : String
{
    return string()
}

//Prints
//Let's invoke a lambda function return Me function here

Type Aliases

Typealiases provide an alternative name for a type.
Instead of typing the function type every time when defining a property, we can use typealias as shown below.


fun main(args: Array<String>) {
    var printFunction: MyFirstAlias = { println(it) }
}

typealias MyFirstAlias =  (String)->Unit

typeAlias Username = String

This definitely enhances the readability of the code.

Annoymous Functions

We’ve seen that lambda expressions can’t explicitly specify a return type as illustrated below.


var sum = { a: Int, b: Int -> a + b }
var result = sum(2,3) //result is an int 5

In order to set the return types explicitly, we can use Annoymous functions.
An annoymous function doesn’t require a name.


fun main(args: Array<String>) {
//Defining
val annoSum = fun(x: Int, y: Int): Int = x + y
//Invoking
print(annoSum(2,3)) //5
}

If the parameter/return type of the annoymous function isn’t defined, it can be inferred just like normal functions.

Let’s use Anonymous functions inside standard library high order functions.


var myList = listOf<Int>(1,2,5,7,6,10)

    myList = myList.filter(fun(item) = (item%2 ==0) )
    println(myList)

filter is a higher order function that checks the given condition over each of the list items.
In the above code, it checks for odd/even over each list integer element.

Note: There are plenty of standard library functions. We’ll look at them and there use cases in a later tutorial.

Kotlin Closures

Closures are functions that can access and modify properties defined outside the scope of the function.
The following closure function is a high order function that calculates the sum of all elements of the list and updates a property defined outside the closure.


var res = 0
myList = listOf(1,2,3,4,5,6,7,8,9,10)
myList.forEach { res+=it }
println(res) //prints 55

This brings an end to this tutorial. We’ll be looking at the standard library higher-order functions in a later tutorial. You can download the sample code of the above tutorial from the link below.

Comments

  1. BELLAM BENARJEE says:

    Can outer class access the private variables of inner class in kotlin

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages