简体   繁体   English

在 Scala 中创建自定义 Seq 集合

[英]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.

相关问题 Seq[A] 的 scala 自定义可遍历 - scala custom traversable of Seq[A] 如何在Scala中创建带有有界类型参数的自定义Seq? - How to create a custom Seq with bounded type parameter in Scala? Scala将Iterable或collection.Seq转换为collection.immutable.Seq - Scala convert Iterable or collection.Seq to collection.immutable.Seq 创建自定义scala集合,其中map默认为返回自定义集合? - Create a custom scala collection where map defaults to returning the custom collection? 如何创建在 Scala 中的集合上扩展的自定义集合? - How to create a custom collection that extends on Sets In Scala? Scala Seq [Seq [SomeClass]]类型不匹配,用于可变集合 - Scala Seq[Seq[SomeClass]] type mismatch for mutable collection 如何从 Java 中的 Java 列表创建 scala.collection.immutable.Seq? - How to create a scala.collection.immutable.Seq from a Java List in Java? 如何将Java Collection / List转换为Scala seq? - How to convert a Java Collection/List to a Scala seq? 如何修复 scala 中的不匹配错误,其中发现:Seq[scala.collection.immutable.Seq required: scala.collection.Seq? - How can I fix mismatch error in scala where the found : Seq[scala.collection.immutable.Seq required: scala.collection.Seq? 在 Scala 中基于增量创建元素序列 - Create a Seq of Elements based on Increment in Scala
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM