[英]Create custom Seq collection in Scala
I want to create a new custom Scala collection from existing Seq
collection.我想从现有的
Seq
集合创建一个新的自定义 Scala 集合。 I have a trait named Ref
which holds the data as below我有一个名为
Ref
的特征,它保存如下数据
trait Ref[A] {
def get: A
def getAsOption: Option[A]
def dataType: Class[A]
// other methods
}
My custom collection named Vec
which is a Sequence
of Ref[A]
(ie Vec[A]
equivalents to Seq[Ref[A]]
), the reason I want to create a custom collection like this because I want to keep the type parameter of Ref
in collection to process in custom methods.我的自定义集合名为
Vec
,它是一个Ref[A]
Sequence
(即Vec[A]
相当于Seq[Ref[A]]
),我想创建这样的自定义集合的原因是因为我想保留类型参数集合中的Ref
以在自定义方法中处理。 My code as below我的代码如下
trait VecLike[A, +Repr <: Seq[Ref[A]]]
extends SeqLike[Ref[A], Repr]
with TraversableLike[Ref[A], Repr] {
// custom methods
}
trait GenVec[A]
extends VecLike[A, Seq[Ref[A]]]
with Seq[Ref[A]]
with PartialFunction[Int, Ref[A]]
abstract class AbstractVec[A](vec: Ref[A]*)
extends AbstractSeq[Ref[A]]
with GenVec[A] {...}
class Vec(vec: Ref[A]*)
extends AbstractVec[A](vec:_*)
with VecLike[A, Vec[A]]
with GenericTraversableTemplate[Ref[A], Seq]
But when I call map()
function但是当我调用
map()
函数时
Vec(1,2,3,4).map(intToString)
It returns a Seq[String]
, the expected result is Vec[String]
.它返回一个
Seq[String]
,预期结果是Vec[String]
。 I also tried to create custom CanBuildFrom
in companion object with SeqFactory[Seq]
but it failed.我还尝试使用
SeqFactory[Seq]
在伴随对象中创建自定义CanBuildFrom
但它失败了。 :( :( :( :(
Can anybody give me some advices about this and how I implement to achieve this?任何人都可以就此给我一些建议以及我如何实施以实现这一目标吗?
You need to implement a custom Builder
and make newBuilder
method in VecLike
return it.您需要实现一个自定义
Builder
,使newBuilder
的方法VecLike
返回。 Check out this page for a very good tutorial on implementing custom collections: http://docs.scala-lang.org/overviews/core/architecture-of-scala-collections.html查看此页面以获得关于实现自定义集合的非常好的教程: http : //docs.scala-lang.org/overviews/core/architecture-of-scala-collections.html
I finished it, that's my mistake I created wrong CanBuildFrom.我完成了,那是我的错误我创建了错误的 CanBuildFrom。 Below is the full implementation with CanBuildFrom (CBF Inspired from Scala GenericCanBuildFrom)
下面是 CanBuildFrom 的完整实现(CBF 灵感来自 Scala GenericCanBuildFrom)
trait VecLike[A, +Repr]
extends SeqLike[Ref[A], Repr]
trait GenVec[A]
extends AnyRef
with VecLike[A, GenVec[A]]
trait Vec[A]
extends GenVec[A]
with VecLike[A, Vec[A]]
abstract class AbstractVec[A](vec: Seq[Ref[A]])
extends AnyRef
with Vec[A]
final class VecImpl[A](vec: Seq[Ref[A]])
extends AbstractVec[A](vec)
object Vec {
private[this] val VecCBFInstance: VecCanBuildFrom[Nothing] = new VecCanBuildFrom[Nothing] {
override def apply() = newBuilder[Nothing]
}
private def VecCBF: VecCanBuildFrom[Nothing] = VecCBFInstance
private class VecCanBuildFrom[A] extends CanBuildFrom[Vec[_], A, Vec[A]] {
def apply(from: Vec[_]) = newBuilder[A]
def apply() = newBuilder[A]
}
implicit def canBuildFrom[A]: CanBuildFrom[Vec[_], A, Vec[A]] = VecCBF.asInstanceOf[VecCanBuildFrom[A]]
}
Now I can receive Vec result instead of Seq after call map() function as below现在我可以在调用 map() 函数后接收 Vec 结果而不是 Seq,如下所示
val vec = Vec(1,2,3,4)
var vecStr : Vec[String] = vec.map(intToString)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.