Using the 'takeIf' Function for Conditional Value Assignment

The 'takeIf' function is a useful standard library function in Kotlin that allows you to conditionally return a value based on a predicate. It helps you simplify code when you want to assign a variable only if it meets certain conditions. This can make your code cleaner and more expressive.

val input: String? = "Hello, Kotlin"
val result: String? = input?.takeIf { it.length > 5 }
// result will be null if input length is not greater than 5