无法找到有序类型的证据参数的隐含值[T]

时间:2012-12-15 23:16:35

标签: scala generics

我实际上已经被封锁了大约4个小时了。我想得到一个按其int值排序的对象列表[String,Int]。函数partiotion工作正常,所以应该是bestN,但是当把它加载到我的解释器中时,我得到:

<console>:15: error: could not find implicit value for evidence parameter of type Ordered[T]

关于我的谓词。有人看到问题是什么吗?我此刻真的很绝望......

这是代码:

def partition[T : Ordered](pred: (T)=>Boolean, list:List[T]): Pair[List[T],List[T]] = {
    list.foldLeft(Pair(List[T](),List[T]()))((pair,x) => if(pred(x))(pair._1, x::pair._2) else (x::pair._1, pair._2))
}

def bestN[T <% Ordered[T]](list:List[T], n:Int): List[T] = {
    list match {
        case pivot::other => {
            println("pivot: " + pivot)
            val (smaller,bigger) = partition(pivot <, list)
            val s = smaller.size
            println(smaller)
            if (s == n) smaller 
            else if (s+1 == n) pivot::smaller
            else if (s < n) bestN(bigger, n-s-1) 
            else bestN(smaller, n)
        }
        case Nil => Nil
    }
}

class OrderedPair[T, V <% Ordered[V]] (t:T, v:V) extends Pair[T,V](t,v) with Ordered[OrderedPair[T,V]] {
    def this(p:Pair[T,V]) = this(p._1, p._2)
    override def compare(that:OrderedPair[T,V]) : Int = this._2.compare(that._2)
}

编辑:第一个函数通过将谓词应用于每个成员将List分为两个,bestN函数应该返回列表列表中最低n个成员的List。这个班级可以使Pairs具有可比性,在这种情况下我想做的是:

val z = List(Pair("alfred",1),Pair("peter",4),Pair("Xaver",1),Pair("Ulf",2),Pair("Alfons",6),Pair("Gulliver",3))

使用这个给定的List我希望得到例如:

bestN(z, 3)

结果:

(("alfred",1), ("Xaver",1), ("Ulf",2))

3 个答案:

答案 0 :(得分:2)

看起来你的分区函数不需要Ordered T,因为它只是调用谓词函数。

以下不起作用(大概)但仅仅编译。代码审查的其他事项将是额外的括号和类似的东西。

package evident

object Test extends App {

  def partition[T](pred: (T)=>Boolean, list:List[T]): Pair[List[T],List[T]] = {
    list.foldLeft(Pair(List[T](),List[T]()))((pair,x) => if(pred(x))(pair._1, x::pair._2) else (x::pair._1, pair._2))
  }

  def bestN[U,V<%Ordered[V]](list:List[(U,V)], n:Int): List[(U,V)] = {
    list match {
      case pivot::other => {
        println(s"pivot: $pivot and rest ${other mkString ","}")
        def cmp(a: (U,V), b: (U,V)) = (a: OrderedPair[U,V]) < (b: OrderedPair[U,V])
        val (smaller,bigger) = partition(((x:(U,V)) => cmp(x, pivot)), list)
        //val (smaller,bigger) = list partition ((x:(U,V)) => cmp(x, pivot))
        println(s"smaller: ${smaller mkString ","} and bigger ${bigger mkString ","}")
        val s = smaller.size
        if (s == n) smaller
        else if (s+1 == n) pivot::smaller
        else if (s < n) bestN(bigger, n-s-1)
        else bestN(smaller, n)
      }
      case Nil => Nil
    }
  }

  implicit class OrderedPair[T, V <% Ordered[V]](tv: (T,V)) extends Pair(tv._1, tv._2) with Ordered[OrderedPair[T,V]] {
    override def compare(that:OrderedPair[T,V]) : Int = this._2.compare(that._2)
  }

  val z = List(Pair("alfred",1),Pair("peter",4),Pair("Xaver",1),Pair("Ulf",2),Pair("Alfons",6),Pair("Gulliver",3))
  println(bestN(z, 3))
}

我发现分区功能难以阅读;你需要一个功能来分割所有的parens。这里有几个公式,它们也使用了过滤器接受的结果左边的拒绝,拒绝正确。

def partition[T](p: T => Boolean, list: List[T]) = 
  ((List.empty[T], List.empty[T]) /: list) { (s, t) =>
    if (p(t)) (t :: s._1, s._2) else (s._1, t :: s._2)
  }
def partition2[T](p: T => Boolean, list: List[T]) =
  ((List.empty[T], List.empty[T]) /: list) {
    case ((is, not), t) if p(t) => (t :: is, not)
    case ((is, not), t)         => (is, t :: not)
  }
// like List.partition
def partition3[T](p: T => Boolean, list: List[T]) = {
  import collection.mutable.ListBuffer
  val is, not = new ListBuffer[T]
  for (t <- list) (if (p(t)) is else not) += t
  (is.toList, not.toList)
}

这可能更接近原始代码的意图:

def bestN[U, V <% Ordered[V]](list: List[(U,V)], n: Int): List[(U,V)] = {
  require(n >= 0)
  require(n <= list.length)
  if (n == 0) Nil
  else if (n == list.length) list
  else list match {
    case pivot :: other =>
      println(s"pivot: $pivot and rest ${other mkString ","}")
      def cmp(x: (U,V)) = x._2 < pivot._2
      val (smaller, bigger) = partition(cmp, other)     // other partition cmp
      println(s"smaller: ${smaller mkString ","} and bigger ${bigger mkString ","}")
      val s = smaller.size
      if (s == n) smaller
      else if (s == 0) pivot :: bestN(bigger, n - 1)
      else if (s < n) smaller ::: bestN(pivot :: bigger, n - s)
      else bestN(smaller, n)
    case Nil => Nil
  }
}

箭头符号更常见:

  val z = List(
    "alfred" -> 1,
    "peter" -> 4,
    "Xaver" -> 1,
    "Ulf" -> 2,
    "Alfons" -> 6,
    "Gulliver" -> 3
  )

答案 1 :(得分:1)

我怀疑我错过了一些东西,但无论如何我都会发布一些代码。

对于bestN,你知道你可以这样做吗?

val listOfPairs = List(Pair("alfred",1),Pair("peter",4),Pair("Xaver",1),Pair("Ulf",2),Pair("Alfons",6),Pair("Gulliver",3))
val bottomThree = listOfPairs.sortBy(_._2).take(3)

这给了你:

List((alfred,1), (Xaver,1), (Ulf,2))

对于partition函数,您可以这样做(假设您希望所有对都低于4):

val partitioned = listOfPairs.partition(_._2 < 4)

给出(左边全部低于4,右边全部更高):

(List((alfred,1), (Xaver,1), (Ulf,2), (Gulliver,3)),List((peter,4), (Alfons,6)))

答案 2 :(得分:0)

与您分享:这有效!非常感谢所有帮助过我的人,你们都很棒!

object Test extends App {

  def partition[T](pred: (T)=>Boolean, list:List[T]): Pair[List[T],List[T]] = {
    list.foldLeft(Pair(List[T](),List[T]()))((pair,x) => if(pred(x))(pair._1, x::pair._2) else (x::pair._1, pair._2))
  }

  def bestN[U,V<%Ordered[V]](list:List[(U,V)], n:Int): List[(U,V)] = {
    list match {
      case pivot::other => {
        def cmp(a: (U,V), b: (U,V)) = (a: OrderedPair[U,V]) <= (b: OrderedPair[U,V])
        val (smaller,bigger) = partition(((x:(U,V)) => cmp(pivot, x)), list)
        val s = smaller.size
        //println(n + " :" + s)
        //println("size:" + smaller.size + "Pivot: " + pivot + " Smaller part: " + smaller + " bigger: " + bigger)
        if (s == n) smaller
        else if (s+1 == n) pivot::smaller
        else if (s < n) bestN(bigger, n-s)
        else bestN(smaller, n)
      }
      case Nil => Nil
    }
  }

  class OrderedPair[T, V <% Ordered[V]](tv: (T,V)) extends Pair(tv._1, tv._2) with Ordered[OrderedPair[T,V]] {
    override def compare(that:OrderedPair[T,V]) : Int = this._2.compare(that._2)
  }
  implicit final def OrderedPair[T, V <% Ordered[V]](p : Pair[T, V]) : OrderedPair[T,V] = new OrderedPair(p)

  val z = List(Pair("alfred",1),Pair("peter",1),Pair("Xaver",1),Pair("Ulf",2),Pair("Alfons",6),Pair("Gulliver",3))
  println(bestN(z, 3))
  println(bestN(z, 4))
  println(bestN(z, 1))
}