anybody else miss a function like `fun <T> M...
# language-proposals
e
anybody else miss a function like
fun <T> MutableList<T>.splice(indices: IntRange, values: Iterable<T>)
? name debatable but that's what as Javascript, Perl, and Rust name the same function, and it's easily accessible via
list[start..end] = values
in Python and Ruby
I've come up with what I think is a reasonable implementation,
Copy code
fun <T> MutableList<T>.splice(indices: IntRange, values: Iterable<T>) {
    var rangeSize = indices.endInclusive - indices.start + 1
    var count = 0
    var iterator = values.iterator()
    while (count < rangeSize && iterator.hasNext()) {
        this[indices.start + count++] = iterator.next()
    }
    if (count < rangeSize) {
        subList(indices.start + count, indices.endInclusive + 1).clear()
    } else if (values is List) {
        addAll(indices.start + count, values.drop(count))
    } else {
        while (iterator.hasNext()) {
            add(indices.start + count++, iterator.next())
        }
    }
}
but haven't really exercised it yet
l
I think you miss the return type in the signature in your snippet.
e
??
: Unit
return type is implied for
{ }
block defined functions
unless you mean that it should return the range that was replaced… no, because that can only be done by a copy. the caller can just as easily
.subList(indices).toList()
first if that's what they want, but it's commonly used enough to warrant being built in here IMO
e
Or to make it have the same syntax as Python and Ruby do:
operator fun <T> MutableList<T>.set(indices: IntRange, values: Iterable<T>)
l
The set operator is less confusing to me compared to "splice"
m
Btw. any reason there is no such operator for get i.e. we can't do python-like
val a = list[2..5]
? I guess maybe it's difficult to choose whether it should be an alias for `subList`vs
slice
.