1

我正在尝试在 Scala 中扩展 SortedMap,但我遇到了 SortedMapLike 和 canBuildFrom 的一些问题(最后一个我什至无法正确输入)。这是一些代码;首先是伴随对象:

object Timeline {
  ...
  def newBuilder[A]: Builder[(Long, A), Timeline[A]] = 
    new ListBuffer[(Long, A)] mapResult fromSeq

  def fromSeq[A](buf: Seq[(Long, A)]): Timeline[A] = 
    new Timeline(buf toMap)

  def empty[A] = Timeline[A](Map[Long, A]())
}

然后上课(是的,我所有的时间线都是从LongA):

final class Timeline[A] private(t: Map[Long, A]) 
  extends SortedMap[Long, A] 
  with SortedMapLike[Long, A, Timeline[A]] {

  private[this] lazy val iMap = 
    TreeMap(t.toArray: _*)(Ordering.fromLessThan[Long](_ > _))

  override def newBuilder: Builder[(Long, A), Timeline[A]] = Timeline.newBuilder
  override def empty: Timeline[A] = Timeline.empty

  def -(key: Long) = Timeline(iMap - key)
  def get(key: Long) = iMap.get(key)
  def rangeImpl(from: Option[Long], until: Option[Long]) = 
    Timeline(iMap.rangeImpl(from, until))
  def iterator = iMap.iterator
  def ordering = iMap.ordering
}

我不确定以上所有内容都是实现此目的的正确方法,但现在是我什至无法正确输入的部分:

implicit def canBuildFrom[A]: CanBuildFrom[Timeline[A], A, Timeline[A]] =
  new CanBuildFrom[Timeline[A], A, Timeline[A]] {
    def apply(): Builder[(Long, A), Timeline[A]] = newBuilder[A]
    def apply(from: Timeline[A]): Builder[(Long, A), Timeline[A]] = newBuilder[A]
  }
4

1 回答 1

1

看来我输入错误canBuildFrom

implicit def canBuildFrom[A]: CanBuildFrom[Timeline[A], (Long, A), Timeline[A]] = 
  new CanBuildFrom[Timeline[A], (Long, A), Timeline[A]] {
    def apply(): Builder[(Long, A), Timeline[A]] = newBuilder[A]
    def apply(from: Timeline[A]): Builder[(Long, A), Timeline[A]] = newBuilder[A]
  }
于 2012-04-12T14:03:01.500 回答