Kotlin 1.2 M2 released

Milestone release 2 of official supported Android language is out now 
10 August 2017   469

Jet Brains works hard to make Kotlin coders happy. Recently, Kotlin 1.2 M2 was released.

This released is focused on stability and bugfixes to Kotlin compiler and tooling. Also, there are some improvements of Kotlin standart library.  It also includes many of the tooling features available in the upcoming release of Kotlin 1.1.4.

Let's see what's inside!

Revised windowed/pairwise operations

Changes to hunked, windowed, pairwise functions:

  • windowed function now has its step parameter defaulting to 1.
    It also gets an additional optional parameter partialWindows, which controls what to do with incomplete windows in the end. By default it is false, which means incomplete windows are dropped.
  • pairwise function name was too confusing, it was unclear how it paired the elements together. Now it is called zipWithNext, so it is more clear that each element is zipped with the next element in a collection.

Common math operations in the standard library

kotlin.math package now released:

  • constants: PI and E;
  • trigonometric: cossintan and inverse of them: acosasinatanatan2;
  • hyperbolic: coshsinhtanh;
  • exponentation: pow (an extension function), sqrthypotexpexpm1;
  • logarithms: loglog2log10lnln1p;
  • rounding:
    • ceilfloortruncateround (half to even) functions;
    • roundToIntroundToLong (half to integer) extension functions;
  • sign and absolute value:
    • abs and sign functions;
    • absoluteValue and sign extension properties;
    • withSign extension function;
  • max and min of two values;
  • binary representation:
    • ulp extension property;
    • nextUpnextDownnextTowards extension functions;
    • toBitstoRawBitsDouble.fromBits (these are in the kotlin package).

The same set of functions (but without constants) is also available for Float arguments.
Most of these functions (except the binary representation group) are also available in JS, thus solving the problem of writing the same calculations for both platforms.

Full release notes:

Android

  • Android Extensions fails to compile when importing synthetic properties for layouts in other modules

Compiler

  • Switch warning to error for java-default method calls within 1.6 target
  • Do not use non-existing class for suspend markers
  • Exception on building gradle project with collection literals
  • Fix collection literals resolve in gradle-based projects

IDE. Inspections and Intentions

  • Circular autofix actions between redundant modality and non-final variable with allopen plugin 
  • "Protected in final" inspection works incorrectly with all-open
  • "Redundant modality" is not reported with all-open
  • Redundant "make open" for abstract class member with all-open

Standard Library

  • windowed function: make default step equals to 1, add partialWindows parameter
  • rename pairwise function to zipWithNext
  • Support math operations in stdlib
  • Provide Double and Float bit conversion functions as extensions

Tools. kapt

  • Kapt3, IC: Kapt does not generate annotation value for constant values in documented types

 

How to convert list to map in Kotlin

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

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()