swap positions list in scala

scala

Solution

An easy (but not very efficient way) of doing this would be

val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)

l.updated(0,l(2)).updated(2,l(0))
res1: List[Int] = List(3, 2, 1)

Problem

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?

Original source