scala 2.9부터 List 객체의 remove 메소드가 사라졌다. Immutable 객체에 remove()가 어색했던 것 같다. 

http://www.scala-lang.org/files/archive/api/2.9.0/#scala.collection.immutable.List



2.8 버전에는 deprecated된 http://www.scala-lang.org/api/2.8.2/의 List의 remove 메소드를 확인할 수 있다.

defremove (p: (A) ⇒ Boolean) List[A]

    deprecated: 
  1. use filterNot' instead



스칼라 2.11로 테스트 해보면 다음과 같다. 


scala> thrill.remove(s=>s.length ==4)

<console>:13: error: value remove is not a member of List[String]

       thrill.remove(s=>s.length ==4)

              ^


scala> thrill.filterNot(s=>s.length==4)

res13: List[String] = List(aa, bb)


scala> thrill.filterNot(s=>s.length==2)

res14: List[String] = List()




참고로 ListBuffer에는 remove()가 존재한다. Buffer는 문맥에 맞게 제공하고 있다. 


http://www.scala-lang.org/api/current/#scala.collection.mutable.ListBuffer

  1. defremove(n: Int)A

    Removes the element on a given index position.

  2. defremove(n: Intcount: Int)Unit

    Permalink

    Removes a given number of elements on a given index position.


  1. def--=(xs: TraversableOnce[A])ListBuffer.this.type

    Removes all elements produced by an iterator from this shrinkable collection.

  2. def-=(elem: A)ListBuffer.this.type

    Remove a single element from this buffer.

  3. def-=(elem1: Aelem2: Aelems: A*)ListBuffer.this.type

    Removes two or more elements from this shrinkable collection.



스칼라 2.11로 테스트 해보면 다음과 같다. 


scala> import scala.collection.mutable.ListBuffer

import scala.collection.mutable.ListBuffer


scala> var thrill = new ListBuffer[String]()

thrill: scala.collection.mutable.ListBuffer[String] = ListBuffer()


scala> thrill += "aaa"

res0: scala.collection.mutable.ListBuffer[String] = ListBuffer(aaa)


scala> thrill += "bbb"

res1: scala.collection.mutable.ListBuffer[String] = ListBuffer(aaa, bbb)


scala> thrill.filter(s => s.length == 2)

res4: scala.collection.mutable.ListBuffer[String] = ListBuffer()


scala> thrill.filter(s => s.length == 3)

res5: scala.collection.mutable.ListBuffer[String] = ListBuffer(aaa, bbb)


scala> thrill.remove(1)

res6: String = bbb


scala> thrill -= "bbb"

res7: scala.collection.mutable.ListBuffer[String] = ListBuffer(aaa)

Posted by '김용환'
,