Is there any reason why there is no: ```fun <T&...
# stdlib
t
Is there any reason why there is no:
Copy code
fun <T> List<T>.slice(fromIndex: Int, toIndex: Int): List<T>
but only:
Copy code
fun <T> List<T>.slice(indices: IntRange): List<T>
Similarly the
List<T>.subList
function has same issue, but flipped around: it has a double index paramter variant, but no
IntRange
invariant.
While
subList
and
slice
behave differently, in both cases we are trying to supply an integer range conceptually. I don't see why each of them only supports either double index parameter, or the
IntRange
, but not both 🤔 Could this simply be an oversight when designing the stdlib?
k
slice() returns elements at the given indexes. sublist() always gives continuous elements from the list as a view. Think about this case:
🌟 1
Copy code
listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).slice(1..5 step 2)
t
Thanks but
step
returns a
IntProgression
which is a subtype of
Iterable
, therefore invoking
Copy code
fun <T> List<T>.slice(indices: Iterable<Int>): List<T>
so its not exactly the issue in here