 Most likely, from Java developers, and especially Android developers, many already know about Kotlin . If not, it's never too late to find out. Especially if Java does not suit you with something like a language - which is probably true - or if you own Scala, but this language does not suit you, which is also not excluded.
 Most likely, from Java developers, and especially Android developers, many already know about Kotlin . If not, it's never too late to find out. Especially if Java does not suit you with something like a language - which is probably true - or if you own Scala, but this language does not suit you, which is also not excluded.fun String.words(): List<String> { return this.split("\\W".toRegex()) } //  ,    return statement fun <T> List<T>.rotate(n: Int): List<T> = drop(n) + take(n) val str = "a quick brown fox jumps over the lazy dog" val words = s.words() val yoda = words.rotate(5) println(yoda.joinToString(" ") // over the lazy dog a quick brown fox jumps  val squares = (1..100) map { i -> i * i } // (1..100).map({i -> i * i }) val multOfThree = squares filter { it % 3 == 0 } //it    -        val list = arrayListOf(1, 2, 3) with(list) { add(4) add(5) add(6) removeIf { it % 2 == 0 } } // with(list, { ... }) @inline annotation that can be used to tag a function. After that, at compilation, the code of this function and its functional arguments will be substituted into the call sites. On the one hand, this gives some new opportunities (non-local return, reified generics), on the other hand, there is a restriction that the functional arguments of the inline-function in its body can only be called or passed to other inline-functions. The main effect of @inline is on performance: fewer function calls occur and, importantly, no anonymous classes and their objects are created for each lambda expression.map and filter . @inline fun <T> Iterable<T>.withEach(action: T.() -> Unit) = forEach { it.action() } //  : var i = 0 val lists = (0..5) map { ArrayList<Int>() } lists.withEach { add(++i) } i will not even get into closure. Just a holiday! val a = someInt() val b = someList() val c = (a % b.size()) butIf (it < 0) { it + b.size() } // (a % b.size()) let { if (it < 0) it + b.size() else it }  fun <T> T.butIf(condition: (T) -> Boolean, thenFunction: (T) -> T): T { if (condition(this)) { return thenFunction(this) } return this }  val c = (a % b.size()).butIf({it < 0}) {it + b.size()}  abstract class _ButIfPrefix<T> constructor(var originalValue: T) { abstract fun then(thenFunction: (T) -> T): T object trueBranch : _ButIfPrefix<Any?>(null) { override final inline fun then(thenFunction: (Any?) -> Any?) = thenFunction(originalValue) } object falseBranch : _ButIfPrefix<Any?>(null) { override final inline fun then(thenFunction: (Any?) -> Any?) = originalValue } } fun <T> T.butIf(condition: (T) -> Boolean): _ButIfPrefix<T> { val result = (if (condition(this)) _ButIfPrefix.trueBranch else _ButIfPrefix.falseBranch) as _ButIfPrefix<T> result.originalValue = this return result } _ButIfPrefix instance. Usage example: val c = (a % b.size()) butIf { it < 0 } then { it + b.size() }  fun <T> T.butIf0(condition: (T) -> Boolean): ((T) -> T) -> T { return inner@ { thenFunction -> return@inner if (condition(this)) thenFunction(this) else this } }  val c = (a % b.size()).butIf { it < 0 } ({ it + b.size() }) @inline . val range = -20000000..20000000 val list = ArrayList<Int>() //warm-up for (i in range) { list add i % 2 } list.clear() val timeBefore = System.currentTimeMillis() for (i in range) { val z = (i % 2) butIf { it < 0 } then { it + 2 } //  list add z } println("${System.currentTimeMillis() - timeBefore} ms")  ... val d = it % 2 val z = if (d < 0) d + 2 else d ... | Implementation | Without inline | C inline | 
|---|---|---|
| Reference | 319 ms | |
| I try | 406 ms | 348 ms | 
| II attempt | 610 ms | 520 ms | 
| II attempt with ThreadLocal | 920 ms | 876 ms | 
| III attempt | 413 ms | 399 ms | 
@inline can help improve the situation if there are first-order functions in your code. In any case, I think you will have good use cases for all this.Source: https://habr.com/ru/post/266817/
All Articles