(email != null) sendEmailTo(email) email?.let { e -> sendEmailTo(e) } getEmail()?.let { sendEmailTo(it) } allows to check the argument for being non-null, not only the receiver
10 } // null val other = 2 other.takeUnless { it > 10 } // 2 returns the receiver object if it does not satisfy the given predicate, otherwise returns null
(!predicate(this)) this else null fun foo(number: Int) { val result = number.takeUnless { it > 10 } ... } Generated code (in the bytecode): fun foo(number: Int) { val result = if (!(number > 10)) number else null ... } inlined code of lambda body
val strings: List<String> = anys.filterIsInstance<String>() inline fun <reified T> List<*>.filterIsInstance(): List<T> { val destination = mutableListOf<T>() for (element in this) { if (element is T) { destination.add(element) } } return destination } inline reified ["one"]
val sequence = listOf(1, 2, 3).asSequence() for (i in sequence) { … } val numbers = generateSequence(0) { it + 1 } numbers.take(5).toList() [0, 1, 2, 3, 4]