特性TraversableLike[+A, +Repr]
は、一部のファンクションがRepr
を返しますが、他のファンクションはファンクションのタイプパラメータThat
を返す一方、コレクションを作成することができます。 ,++
などの機能がデフォルトでThat
、Repr
などと推定される場合は、CustomCollection[A]
を定義する方法はありますか?ここで mapがデフォルトでカスタムコレクションを返すカスタムスカラコレクションを作成しますか?
CustomCollection(2, 3, 4)
する最後の行をしたいと思います
case class CustomCollection[A](list: List[A]) extends TraversableLike[A, CustomCollection[A]] {
protected[this] def newBuilder = new CustomCollectionBuilder[A]
def foreach[U](f: (A) => U) {list foreach f}
def seq = list
}
class CustomCollectionBuilder[A] extends mutable.Builder[A, CustomCollection[A]] {
private val list = new mutable.ListBuffer[A]()
def += (elem: A): this.type = {
list += elem
this
}
def clear() {list.clear()}
def result(): CustomCollection[A] = CustomCollection(list.result())
}
object CustomCollection extends App {
val customCollection = CustomCollection(List(1, 2, 3))
println(customCollection filter {x => x == 1}) // CustomCollection(1)
println(customCollection map {x => x + 1}) // non-empty iterator
}
。
私はちょうど3分後の背後にあることのためだけに1 OM-NOM-NOMにログイン。 「タイムリーな編集」とは思われません。親指のロゴ付きのボタン。 –