Kotlin 1.2 EAP now available

Good news for all Kotlin coders - early access program of Kotlin 1.2 available
19 July 2017   758
Kotlin

Statically-typed programming language that runs on the Java Virtual Machine and also can be compiled to JavaScript source code.

Kotlin team recently announced the start of the early access program for Kotlin 1.2. First milestone 1.2M1 was out recently. This is reported by official Kotlin Blog

Let's have a good look at the most interesting features of EAP.

Language feature: array literals in annotations

The single notable language feature proposed in this milestone is array literals, whose usages are constrained to annotation arguments.

In order to specify an array of values, coder had to write something like this:

@CacheConfig(cacheNames = arrayOf("books", "default"))
public class BookRepositoryImpl {
    // ....
}

Now, in Kotlin 1.2 a literal can be used instead of the arrayOf function:

@CacheConfig(cacheNames = ["books", "default"])

public class BookRepositoryImpl {

    // ....

}

Additionally, an IDE inspection will propose you to use the new syntax of collection literals where appropriate.

Inline function with optional functional parameters

Earlier, it was impossible to declare an inline higher order function with an optional functional parameter like the following:

inline fun <E> List<E>.printItems(transform: (E) -> String = { it.toString() })

One had to either make the function itself non-inline or mark the functional parameter as noinline. In either case that defeated the purpose of functional parameter inlining.

Now this case is fully supported.

Standard Library API

This release features two KEEPs (Kotlin Evolution and Enhancement Proposals) regarding the standard library API.

The first is KEEP-49 . It provides additional operations and conversion extensions for BigIntegerand BigDecimal types.

The second one is KEEP-11 which covers a number of functions related to partitioning collections and sequences:

  • chunked(size: Int) extension function partitions a collection into blocks of the given size;
  • windowed(size: Int, step: Int) takes a window of the given size and moves it along the collection with the given step returning the sublists of elements falling into each window;
  • pairwise() extension returns all subsequent pairs in a collection.

JavaScript

Earlier in 1.1.2 release developers have introduced the opt-in JS typed arrays support. When enabled it translates Kotlin primitive arrays, such as IntArrayDoubleArray etc, into JavaScript typed arrays. Now the opt-in is no longer required as this support is enabled by default.

You can find more info at changelog and Kotlin Blog.

How to convert list to map in Kotlin

Two ways to convert list to map in Kotlin, with code examples
31 October 2017   508

For example, you have a list of strings like:

val list = listOf("a", "b", "c", "d")

and you need to convert it to a map, where the strings are the keys.

There are two ways to do this:

The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map:

val map = friends.associateBy({it.facebookId}, {it.points})

The second, less performant, is to use the standard map function to create a list of Pair which can be used by toMap to generate the final map:

val map = friends.map { it.facebookId to it.points }.toMap()