Given an var x: Array[Seq[Any]]
, what would be the most efficient (fast, in-place if possible?) way to remove the first element from each row?
I've tried the following but it didn't work - probably because of immutability...
for (row <- x.indices)
x(row).drop(1)
First off,
Array
s are mutable, and I'm guessing you're intending to change the elements ofx
, rather than replacing the entire array with a new array object, so it makes more sense to useval
instead ofvar
:Since you said your
Seq
objects are immutable, then you need to make sure you are setting the values in your array. This will work:This can be written in nicer ways. For example, you can use
transform
to map all the values of your array with a function:This updates in-place, unlike
map
, which will leave the old array unmodified and return a new array.EDIT: I started to speculate on which method would be faster or more efficient, but the more I think about it, the more I realize I'm not sure. Both should have acceptable performance for most use cases.