Using Inline Functions for Performance Optimization

Inline functions in Kotlin allow you to reduce the overhead associated with function calls, particularly when using higher-order functions. When a function is marked as inline, the compiler replaces the function call with the actual function body during compilation. This can lead to performance improvements, especially in tight loops or frequently called functions.

inline fun  List.customFilter(predicate: (T) -> Boolean): List {
    val result = mutableListOf()
    for (item in this) {
        if (predicate(item)) {
            result.add(item)
        }
    }
    return result
}

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val evenNumbers = numbers.customFilter { it % 2 == 0 }
    println(evenNumbers) // Output: [2, 4]
}