Kotlin Advanced Programming Part 2

A journey to Kotlin Functional Programming

Image for post
Image for post

If you are new to Kotlin please check out my previous posts Kotlin Guide for Beginners and Kotlin Advanced Programming and move forward for better understanding. In this post let’s check some advanced concepts related to the functional programming of Kotlin.

Unit-returning functions

In Kotlin every function has function type. If the function returns nothing then the function return type is Unit. It’s optional to specify the Unit return type explicitly. It’s similar to void in the case of Java.


Functions with Varargs

A function that accepts a variable number of arguments (varargs).

The above function can be called by passing n number of params of type T

val list = listConversion("abc","xyz","pqr")

Infix notation

infix is a keyword in Kotlin. We can use this infix keyword with functions so that we can call these functions on the class object without using period “.” and parenthesis(). This is called infix notation(omitting the dot and the parentheses for the function call). infix notations help’s in increasing the readability of the program. Let’s check out a simple example for a better understanding

To make a function infix notation enable we need to add a keyword infix before it. Unlike other functions, there are three requirements that must be met to be an infix function. Those are:

  • They must be member functions or extension functions;
  • They must have a single parameter;
  • The parameter must not accept a variable number of arguments (no varargs)and must have no default value.

Infix function calls have lower precedence than the arithmetic operators, type casts, and the rangeTo operator. On the other hand, infix function call’s precedence is higher than that of the boolean operators && and ||, is- and in-checks, and some other operators. Check out more details on Kotlin docs

What are the Function types

In Kotlin every function has a function type. Kotlin uses function types to represent the type of function. The function type is based on its parameters and return type. The function type is indicated with a parenthesized parameter type list and an arrow to the return type. The syntax of function type is

(parameter1Type, parameter2Type) -> returnType

On the left-hand side, it contains parameters between the parenthesis and on the right-hand side it has the return type, both are connected by an arrow

Let’s checkout few function types:

  • ()->Unit —The function type that takes no parameters and returns nothing
  • (Int, Int)->Int— The function type that takes two Int parameters and returns an Int value
  • A.(B) -> C represents functions that can be called on a receiver object of A with a parameter of B and return a value of C.
  • ()->() -> () a function that returns other functions which in turn returns nothing.

We can reference the function itself by prefixing its name with ::

Function Literals

Lambda expressions and anonymous functions are ‘function literals’, i.e. functions that are not declared but passed immediately as an expression. Let’s get some insight into them.

Lambda Expression

A function with no name is called an anonymous function. Lambda expressions are similar to anonymous functions consisting of a block of statements to be executed. Kotlin Lambdas are very similar to Java Lambdas. The syntax of a lambda expression is

Lambda expression is surrounded by curly braces {...} and the code that needs to be executed and the parameters go inside these braces. Parameters types are optional if they can be inferred. Lambda’s body goes after the arrow ->. Let’s check a simple example:

Sample lambda expression

In the first case the types of a,b inside {} are inferred from the type specified on the left-hand side and vice versa goes for a second. If there was only one parameter then it can be omitted along with the arrow -> and we can use it as a reference to the single parameter. it is the implicit name of a single parameter if not specified.

var incrementExpression: (Int) -> Int = { it + 1 }

The final expression is the value that will be returned after a lambda is executed:

If the lambda parameter was not being used, we can place an underscore instead of any name:

map.forEach { _, mapValue-> println("$mapValue!") }

Anonymous Functions

An anonymous function is much like a normal function except that name of the function will not be specified. The body of an anonymous function can be either an expression or a block. Its nothing but a normal function without a name.

anonymous function

Invoking a function type instance

A value of a function type can be invoked by using its invoke(..) operator f.invoke(x) or just f(x).

The result of all equations is the same i.e, 20.


By now you should have a basic idea of functional programming in Kotlin. Please stay tuned I will keep writing more posts on Kotlin language series.

Continue reading on Kotlin:


Kotlin Docs

Please let me know your suggestions and comments.

You can find me on Medium and LinkedIn

Thanks for reading…

Android Dev at Toppr. Interested in Traveling, App development. Based in Hyderabad, India. Catch me at https://about.me/satyapavankumar

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store