在Scala中扩展SortedMap

时间:2012-04-12 00:06:36

标签: scala collections

我正在尝试在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]
  }

1 个答案:

答案 0 :(得分: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]
  }