scala / collection-strawman

Implementation of the new Scala 2.13 Collections
Apache License 2.0
200 stars 72 forks source link

In-Place `dropRight` for `ListBuffer` #485

Closed hosseinhaeri closed 6 years ago

hosseinhaeri commented 6 years ago

ListBuffer does have the familiar dropRight method. It also has the in-place --= expected of a Buffer -- albeit only taking a TraversableOnce[A]. Is there any hope it can also have an in-place dropRight? A syntax like myListBuffer --= n feels convenient to me (where n is the number of elements to drop from the end of myListBuffer).

I do already know that there is the possibility of getting the same end-result using

myListBuffer --= myListBuffer.drop(myListBuffer.size - n)

But, that’s an overkill because the temporary collection produced by myListBuffer.drop(myListBuffer.size - n) is more than what is required for doing simply an in-place dropRight. Merely n suffices.

julienrf commented 6 years ago

Note that you could avoid the creation of a temporary collection as follows:

myListBuffer --= myListBuffer.view.drop(myListBuffer.size - n)

But still, it would be more convenient to have high-level operations to trim Shrinkable collections.

hosseinhaeri commented 6 years ago

Thanks. I wasn't actually aware of the view facility. I will use that for now. But, I'll look forward the convenient syntax.

tim-ruhland commented 6 years ago

ListBuffer does have a dropRightInPlace method defined. Is the issue to create the --= alias for dropRightInPlace?

julienrf commented 6 years ago

Actually I forgot to check but all Buffers have dropRightInPlace: https://static.javadoc.io/ch.epfl.scala/collection-strawman_2.13.0-M2/0.9.0/strawman/collection/mutable/Buffer.html#dropRightInPlace(n:Int):Buffer.this.type

hosseinhaeri commented 6 years ago

Thank you for pointing out dropRightInPlace. It would be of even more interest to have that alised into --= too.