I have an immutable list and I need to swap locations in it. Is there any easy way of doing it?
Below is my code:
def swap(i:Int, j:Int,li:List[T]):List[T]={
if(i>=li.size && j >=li.size)
throw new Error("invalie argument");
val f = li(i)
li(i) = li(j) //wont work
li(j) = f;//wont work
li;
}
Initially, i tried it by converting it to an Array, changing the positions and then converting it to a List again. Any easy way?
An easy (but not very efficient way) of doing this would be