Replace element in List with scala

ScalaReplace

Scala Problem Overview


How do you replace an element by index with an immutable List.

E.g.

val list = 1 :: 2 ::3 :: 4 :: List()

list.replace(2, 5)

Scala Solutions


Solution 1 - Scala

If you want to replace index 2, then

list.updated(2,5)    // Gives 1 :: 2 :: 5 :: 4 :: Nil

If you want to find every place where there's a 2 and put a 5 in instead,

list.map { case 2 => 5; case x => x }  // 1 :: 5 :: 3 :: 4 :: Nil

In both cases, you're not really "replacing", you're returning a new list that has a different element(s) at that (those) position(s).

Solution 2 - Scala

In addition to what has been said before, you can use patch function that replaces sub-sequences of a sequence:

scala> val list = List(1, 2, 3, 4)
list: List[Int] = List(1, 2, 3, 4)

scala> list.patch(2, Seq(5), 1) // replaces one element of the initial sequence
res0: List[Int] = List(1, 2, 5, 4)

scala> list.patch(2, Seq(5), 2) // replaces two elements of the initial sequence
res1: List[Int] = List(1, 2, 5)

scala> list.patch(2, Seq(5), 0) // adds a new element
res2: List[Int] = List(1, 2, 5, 3, 4)

Solution 3 - Scala

You can use list.updated(2,5) (which is a method on Seq).

It's probably better to use a scala.collection.immutable.Vector for this purpose, becuase updates on Vector take (I think) constant time.

Solution 4 - Scala

You can use map to generate a new list , like this :

@ list
res20: List[Int] = List(1, 2, 3, 4, 4, 5, 4)
@ list.map(e => if(e==4) 0 else e)
res21: List[Int] = List(1, 2, 3, 0, 0, 5, 0)

Solution 5 - Scala

It can also be achieved using patch function as

scala> var l = List(11,20,24,31,35)

l: List[Int] = List(11, 20, 24, 31, 35)

scala> l.patch(2,List(27),1)

res35: List[Int] = List(11, 20, 27, 31, 35)

where 2 is the position where we are looking to add the value, List(27) is the value we are adding to the list and 1 is the number of elements to be replaced from the original list.

Solution 6 - Scala

If you do a lot of such replacements, it is better to use a muttable class or Array.

Solution 7 - Scala

following is a simple example of String replacement in scala List, you can do similar for other types of data

    scala> val original: List[String] = List("a","b")
    
    original: List[String] = List(a, b)
    
    scala> val replace = original.map(x => if(x.equals("a")) "c" else x)
    
    replace: List[String] = List(c, b)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBerlin BrownView Question on Stackoverflow
Solution 1 - ScalaRex KerrView Answer on Stackoverflow
Solution 2 - ScalaVasil RemeniukView Answer on Stackoverflow
Solution 3 - ScalaKen BloomView Answer on Stackoverflow
Solution 4 - Scaladamon-linView Answer on Stackoverflow
Solution 5 - ScalanambasthaView Answer on Stackoverflow
Solution 6 - Scalav6akView Answer on Stackoverflow
Solution 7 - ScalapraxnetView Answer on Stackoverflow