Using the 'take' Function for Collection Truncation

The 'take' function in Kotlin can be used to retrieve a specified number of elements from the beginning of a collection. This is particularly useful when you want to limit the size of a list or show only the first few items of a larger dataset, enhancing performance and readability in your application.

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