This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main(args: Array<String>) { | |
| example(1 to Example()) { | |
| x() | |
| } | |
| example(1 to "string") { | |
| substring(2) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |