Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take last n element in Kotlin

I have a list on which I'd like to apply some transformations, but excluding the first 2 elements. How can I do it the nicest way? Something like this:

list.reversed().take(list.size - 2)...(my transformations)

or

list.excludeFirstN(2)...(my transformations)
like image 995
LordScone Avatar asked Feb 20 '16 11:02

LordScone


People also ask

How do you get the last element of String in Kotlin?

Returns a subsequence of this char sequence containing the last n characters from this char sequence, or the entire char sequence if this char sequence is shorter.

How do I find the last element in a list in Kotlin?

Returns the last valid index for the array. Returns the index of the last item in the list or -1 if the list is empty.

What is .USE in Kotlin?

} use accepts a function literal and is defined as an extension on an instance of closeable. It will close down the resource, just like the try-with-resources construct, after the function has been completed, whether an exception was raised or not.


1 Answers

Oh, I found the drop() function.

like image 143
LordScone Avatar answered Oct 09 '22 04:10

LordScone