Kotlin provides extension functions for retrieving collection parts. A member function defined outside the class is called an extension function. These extension functions can provide a variety of ways to select different items from the list.
Four extension functions are:
- Take and Drop
Slice functions work just like in other languages which returns a list of items with given indices. These indices can be passed as range or integer values.
Kotlin example to demonstrate slice –
[banana, cherries, dragon_fruit, egg_fruit] [apple, cherries, egg_fruit] [banana, cherries, egg_fruit]
Take and Drop
As the name suggests take() function can take specified number of items from the start. If we want to grab items from last we can call takeLast() function.
Similarly, drop() function takes all items except the specified number of items from the start and dropLast() takes all the items except the specified number of items from the last.
Kotlin example to demonstrate take and drop –
[apple, banana, cherries] [dragon_fruit, egg_fruit, fig] [egg_fruit, fig] [apple, banana]
There are four functions in take and drop with which we can use predicates to define number of items to take and drop.
Kotlin program –
[apple, banana, cherries] [dragon_fruit, egg_fruit, fig] [banana, cherries, dragon_fruit, egg_fruit, fig] [apple, banana]
Chunking is a function by which individual pieces of an list are broken down and then grouped together into specified chunk size. chunk() function takes a single argument an returns lists of that chunk size.
We can also apply transformation for the returned chunks. In order to do that, we provide the transformation as a lambda function when calling chunked(). When chunked() is called with a transformation, the chunks are short-living Lists that should be used in the lambda expression for another purpose.
Kotlin program of using chunk() function –
[[apple, banana, cherries], [dragon_fruit, egg_fruit, fig]] [3, 12, 21, 30]
The windowed() function can fetch all possible ranges of collection of items on a given size. It returns a list of element ranges that you would see at the collection using sliding window of the particular size.
It has optional two parameters:
step: distance between first item of every window. By default the value is 1, so the result contains windows starting from all elements.
partialWindows: if true includes windows of smaller size left at the end.
Kotlin program to demonstrate windowed() function –
[[apple, banana, cherries], [banana, cherries, orange]] [[apple, banana, cherries], [cherries, orange]]
There is a zipWithNext() function to build two element window, can also be called with a transformation.
Kotlin program to demonstrate zipWithNext() –
[(apple, banana), (banana, cherries), (cherries, dragon_fruit), (dragon_fruit, egg_fruit), (egg_fruit, fig)]
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.