What is an idiomatic Scala way to "remove" one element from an immutable List?
You could use the filterNot
method.
val data = "test"
list = List("this", "is", "a", "test")
list.filterNot(elm => elm == data)
I haven't seen this possibility in the answers above, so:
scala> def remove(num: Int, list: List[Int]) = list diff List(num)
remove: (num: Int,list: List[Int])List[Int]
scala> remove(2,List(1,2,3,4,5))
res2: List[Int] = List(1, 3, 4, 5)
Edit:
scala> remove(2,List(2,2,2))
res0: List[Int] = List(2, 2)
Like a charm :-).