Kotlin and lambda functions: A few examples

Kotlin is seriously much less verbose than Java. Some people would state that they prefer the verbosity of Java, but conciseness of code is extremely attractive. Kotlin has many time-saving and code-saving features, and in my opinion the greatest among them is syntactic sugar “it” for use with lists.

Let’s say that I wanted to fill a list of integers with the value of zero for each element. It would not be so challenging if we were talking about an integer array in Java, since initializing an integer array with only a size but no specification of the actual values of each element will default to each element being made equal to zero. This is nice, but what happens in the context of a list of integers?

List<Integer> list = new ArrayList<Integer>(10);

In Java, the above line does not produced a list of 10 integers prefilled as zeros. Whereas the following line (an array of integer primitives) does have such prefilled behavior:

int[] array = new int[10];

With our List<> example, one might assume that the problem is that the problem arises from the use of the Integer wrapper for int, and it’d be reasonable to assume that, but one also cannot replace List<Integer> with List<int> as this would not compile.

Kotlin in terms of syntax is very friendly in comparison to Java. Much ink has been spilt writing about this. Let’s see the use of ‘it’ in prefilling a list of ten elements with zeros:

val list = List(10){0}

This is Kotlin’s lambda syntax. This code means a list, initialized to be immutable, with a size of ten, is given all zeros for values.

Use of ‘it’
val another = List(10) {it + 1}

The above code prefills a list of ten elements with 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. We can infer from the above example, and the below examples, that ‘it’ is a kind of placeholder.

val myList = List(26) {'a' + it}

Above: a list of 26 elements is populated with characters ‘a’ through ‘z’ by adding to ‘a’ the value of it.

val isPlus:(Int) -> Boolean = { it > 0}

println( isPlus(27) )

It might not be entirely obvious from the perspective of a Java programmer, but isPlus is actually a function. It is a bit confusing, but what is described here is a function called isPlus with a single integer parameter that evaluates whether or not that parameter is positive or negative. If it is greater than zero, then the function isPlus returns true. -> Boolean = is merely the stated return type. Here it holds the value of the integer parameter that is passed in.

val list = listOf(1,2,3,4)
val evens = list.filter {it % 2 == 0}

The above code may be somewhat self-explanatory. We create a list called ‘evens’ that contains only the elements from ‘list’ that are divisible by two.

fun main(args : Array<String>) {

val names = listOf("Raj", "Betty", "Ernst", "Takemura", "Jojo")
val ages = listOf(21, 20, 25, 34, 23)

fun people() : List<Person> = names.zip(ages) {name, age -> Person(name, age) }

}

data class Person(private val name : String, private val age : Int)

This in-line lambda function is a lot like the isPlus example but a bittle more verbose. We know in this specific case that people() is a function on account of it having fun … before it. In this case we are creating a list of Person objects (there is a data class called Person which has a default constructor that takes one string and one integer) from two lists that we are zipping together. In the function people(), the elements of the lists names and ages are treated almost like key-value pairs, or associated pairs. To me, this is a kind of computer magic.

Additional Reading

Use of curly braces after function calls

Leave a comment