小编Tom*_*ong的帖子

scala - 使用"sortBy"时混淆"发散隐式扩展"错误

我想知道为什么List(3,2,1).toIndexedSeq.sortBy(x=>x)不起作用:

scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong
<console>:8: error: missing parameter type
              List(3,2,1).toIndexedSeq.sortBy(x=>x)
                                              ^
<console>:8: error: diverging implicit expansion for type scala.math.Ordering[B]
starting with method Tuple9 in object Ordering
              List(3,2,1).toIndexedSeq.sortBy(x=>x)
                                             ^

scala> Vector(3,2,1).sortBy(x=>x) // OK
res: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

scala> Vector(3,2,1).asInstanceOf[IndexedSeq[Int]].sortBy(x=>x) // OK
res: IndexedSeq[Int] = Vector(1, 2, 3)

scala> List(3,2,1).toIndexedSeq.sortBy((x:Int)=>x) // OK
res: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

scala implicit scala-collections

9
推荐指数
1
解决办法
4263
查看次数

如何调用Scala的Queue.enqueue(iter:Iterable [B])?

在Scala中immutable.Queue,有两种方法都命名为enqueue:

  /** Creates a new queue with element added at the end
   *  of the old queue.
   *
   *  @param  elem        the element to insert
   */
  def enqueue[B >: A](elem: B) = new Queue(elem :: in, out)

  /** Returns a new queue with all elements provided by an `Iterable` object
   *  added at the end of the queue.
   *
   *  The elements are prepended in the order they are given out by the
   *  iterator.
   * …
Run Code Online (Sandbox Code Playgroud)

scala

5
推荐指数
1
解决办法
918
查看次数

标签 统计

scala ×2

implicit ×1

scala-collections ×1