Kotlin
Kotlin

You may try this with Kotlin Playground -> https://play.kotlinlang.org

Swap two values w/o temp variable

    var a = 1
    var b = 2

    a = b.also { b = a }

    println(a) // print 2
    println(b) // print 1
a = 2
b = 1

a = b.also { b = a } is equivalent to followings:

    var a = 1
    var b = 2
    var temp = 0
    
    temp = a
    a = b
    b = temp

Enum Reverse Look-up

enum class Type(val value: Int) {
    A(1),
    B(2),
    C(3);
}

Type.values().associateBy(Type::value).forEach(::println) // returns {1=A, 2=B, 3=C}
1=A
2=B
3=C
Last modified: June 5, 2020

Author