Skip to content

Instantly share code, notes, and snippets.

fun main(args: Array<String>) {
example(1 to Example()) {
x()
}
example(1 to "string") {
substring(2)
}
}
operator fun <T, A, B, C> T.getSlice(first: A, last: B, step: C)
operator fun <T, A, B, C, S> T.setSlice(first: A, last: B, step: C, value: S)
and we can use them:
operator fun <T> List<T>.getSlice(first: Int = 0, last: Int = size - 1, step: Int = 1)
list[first:]
list[first:last]
list[first:last:step]
list[:last]
list[:last:step]
list[first: :step]
val list = listOf(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
list[1, 3] // [3, 5, 7]
list[4, 1] // [11, 7, 5, 3]
list[2, -1] // [5, 7, 11, 13, 17, 19, 23, 29]
list[3, 0, 1] // [7, 5, 3, 2]
list[-1, 1, 2] // [29, 19, 13, 7, 3]
val list = listOf(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
list[!5] // [13, 17, 19, 23, 29]
list[!1/3] // [3, 5, 7]
list[!4/1] // [11, 7, 5, 3]
list[!2/-1] // [5, 7, 11, 13, 17, 19, 23, 29]
list[!3/0/1] // [7, 5, 3, 2]
list[!-1/1/2] // [29, 19, 13, 7, 3]
fun lsxUsage() {
val list = listOf(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
println(list[1..3]) // [3, 5, 7]
println(list[1 til 3]) // [3, 5]
println(list[4..1]) // [11, 7, 5, 3]
println(list[4 til 1]) // [11, 7, 5]
println(list[2..-1]) // [5, 7, 11, 13, 17, 19, 23, 29]
println(list[2 til -1]) // [5, 7, 11, 13, 17, 19, 23]
println(list[3..0 by 1]) // [7, 5, 3, 2]