Using the 'takeLast' Function for Retrieving the Last Elements of a Collection

The 'takeLast' function in Kotlin is useful when you want to obtain a specified number of elements from the end of a collection. This can be particularly handy in scenarios such as displaying recent items, logs, or any other context where the latest entries are of interest.

val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val lastThreeNumbers = numbers.takeLast(3)
println(lastThreeNumbers) // Output: [8, 9, 10]