learner
learner

Reputation: 11790

scala "remove" not working

According to page 44 of the book "Programming in Scala", there exists a remove function for the list data structure. However, when I try the example in my interpreter, I keep getting errors. Does anyone know why? Here is a sample

scala> val x = List(1,2,3,4,5,6,7,8,9)
x: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> x.remove(_ < 5)
<console>:9: error: value remove is not a member of List[Int]
              x.remove(_ < 5)
                ^

scala> x.remove(s => s == 5)
<console>:9: error: value remove is not a member of List[Int]
              x.remove(s => s == 5)
                ^

scala> val y = List("apple","Oranges","pine","sol")
y: List[String] = List(apple, Oranges, pine, sol)

scala> y.remove(s => s.length ==4)
<console>:9: error: value remove is not a member of List[String]
              y.remove(s => s.length ==4)

Upvotes: 4

Views: 1953

Answers (2)

drexin
drexin

Reputation: 24403

List had a remove method in earlier versions, but it has been deprecated in 2.8 and removed in 2.9. Use filterNot instead.

Upvotes: 10

Brian Agnew
Brian Agnew

Reputation: 272337

ListBuffer has a remove method, but not List. See here for info on how to idiomatically remove from an immutable List (obviously creating a new List!)

Upvotes: 4

Related Questions