创建自定义scala集合,其中map默认返回自定义集合?
trait创建自定义scala集合,其中map默认返回自定义集合?,scala,collections,type-inference,scala-collections,scala-2.10,Scala,Collections,Type Inference,Scala Collections,Scala 2.10,traitTraversableLike[+A,+Repr]允许一个集合,其中一些函数将返回一个Repr,而其他函数则继续返回函数上的类型参数。有没有办法定义一个CustomCollection[a],其中像map、++,以及其他函数都将默认,如果没有其他推断,则将定义为Repr 下面是一段代码片段,希望能描述我想要的: case class CustomCollection[A](list: List[A]) extends TraversableLike[A, CustomCollectio
TraversableLike[+A,+Repr]
允许一个集合,其中一些函数将返回一个Repr
,而其他函数则继续返回函数上的类型参数。有没有办法定义一个CustomCollection[a]
,其中像map
、++
,以及其他函数都将默认,如果没有其他推断,则将定义为Repr
下面是一段代码片段,希望能描述我想要的:
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
}
我希望最后一行是CustomCollection(2,3,4)
您需要设置一个伴生对象,该对象提供优化的CanBuildFrom
实例:
import collection.TraversableLike
import collection.generic.{CanBuildFrom, GenericCompanion, GenericTraversableTemplate,
TraversableFactory}
import collection.mutable.{Builder, ListBuffer}
object CustomCollection extends TraversableFactory[CustomCollection] {
def newBuilder[A] = new CustomCollectionBuilder[A]
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, CustomCollection[A]] =
new CanBuildFrom[Coll, A, CustomCollection[A]] {
def apply(): Builder[A, CustomCollection[A]] = new CustomCollectionBuilder()
def apply(from: Coll): Builder[A, CustomCollection[A]] = apply()
}
}
case class CustomCollection[A](list: List[A]) extends Traversable[A]
with TraversableLike[A, CustomCollection[A]]
with GenericTraversableTemplate[A, CustomCollection] {
override def companion: GenericCompanion[CustomCollection] = CustomCollection
def foreach[U](f: A => U) { list foreach f }
override def seq = list
}
class CustomCollectionBuilder[A] extends Builder[A, CustomCollection[A]] {
private val list = new ListBuffer[A]()
def += (elem: A): this.type = {
list += elem
this
}
def clear() {list.clear()}
def result(): CustomCollection[A] = CustomCollection(list.result())
}
val customCollection = CustomCollection(List(1, 2, 3))
val f = customCollection filter {x => x == 1} // CustomCollection[Int]
val m = customCollection map {x => x + 1} // CustomCollection[Int]
我只以+1的名义登录,因为我只落后帖子三分钟。似乎没有一个带有竖起大拇指标志的“及时编辑”按钮。回答得很好!我觉得我需要使用cbf,但我找不到最通用的方法。有什么地方可以让我读到更多关于collections api的细节吗?也就是说,我怎么知道除了扩展TraversableLike
之外,我还需要扩展GenericTraversableTemplate
?@dicarlo2-我通常会查看与我正在做的最相似的类的源代码,并复制它们混合在一起的内容。我发现这种方法比任何其他方法都更可靠,因为我不知道每个特征提供了什么功能(以及您希望所有菱形继承模式如何解决)。@RexKerr-Heh,这和我通常做的一样,在发布了这个问题之后,在我不得不离开计算机之前,我一直到TraversableFactory
,谁知道我会浏览多少文件来找到所有菱形继承模式。遗憾的是,没有更好的参考资料,文档也不能说明每个特征之间的关系。@dicarlo2-我认为集合库是伪装成“这张图片中有多少三角形”的谜题之一。最好的办法可能是保留现有集合,如果你需要额外的功能,尝试使用Scala 2.10的IsTraversableLike
helper将它们添加到类型类样式中。