The drop functions

There may be some scenarios when you want to drop a portion (say, the first 5 or the last 10) of the collection and work on the remaining parts. Kotlin's collection framework provides you with a set of the drop functions that can help you in these scenarios. Have a look at the following program:

fun main(args: Array<String>) { 
    val list = 1.until(50).toList() 
 
    println("list.drop(25) -> ${list.drop(25)}")//(1) 
    println("list.dropLast(25) -> ${list.dropLast(25)}")//(2) 
} 

In the preceding program, we've dropped the first 25 items from the list on comment (1), and on comment (2), I've dropped the last 25 items.

The following screenshot shows the output of the program:

Worked perfectly, didn't it?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.27.131