简体   繁体   English

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

[英]could not find implicit value for evidence parameter of type Ordered[T]

I am actually blocked on this for about 4 hours now. 我实际上已经被封锁了大约4个小时了。 I want to get a List of Pairs[String, Int] ordered by their int value. 我想得到一个按其int值排序的对象列表[String,Int]。 The function partiotion works fine, so should the bestN, but when loading this into my interpreter I get: 函数partiotion工作正常,所以应该是bestN,但是当把它加载到我的解释器中时,我得到:

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

on my predicate. 在我的谓词上。 Does someone see what the problem is? 有人看到问题是什么吗? I am really desperate at the moment... 我此刻真的很绝望......

This is the code: 这是代码:

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)
}

Edit: The first function divides a List into two by applying the predicate to every member, the bestN function should return a List of the lowest n members of the list list. 编辑:第一个函数通过将谓词应用于每个成员将List分为两个,bestN函数应返回列表列表中最低n个成员的List。 And the class is there to make Pairs comparable, in this case what I want do do is: 这个班级可以使Pairs具有可比性,在这种情况下我想做的是:

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

with this given List I want to get for example with: 有了这个给定的List我想得到例如:

bestN(z, 3)

the result: 结果:

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

It looks like you don't need an Ordered T on your partition function, since it just invokes the predicate function. 看起来您的分区函数不需要Ordered T,因为它只调用谓词函数。

The following doesn't work (presumably) but merely compiles. 以下不起作用(大概)但仅仅编译。 Other matters for code review would be the extra braces and stuff like that. 代码审查的其他事项将是额外的括号和类似的东西。

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))
}

I found the partition function hard to read; 我发现分区功能难以阅读; you need a function to partition all the parens. 你需要一个功能来分割所有的parens。 Here are a couple of formulations, which also use the convention that results accepted by the filter go left, rejects go right. 这里有几个公式,它们也使用过滤器接受的结果左边的约定,拒绝正确。

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)
}

This might be closer to what the original code intended: 这可能更接近原始代码的意图:

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
  }
}

Arrow notation is more usual: 箭头符号更常见:

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

I suspect I am missing something, but I'll post a bit of code anyway. 我怀疑我错过了什么,但无论如何我会发布一些代码。

For bestN , you know you can just do this? 对于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)

Which gives you: 哪个给你:

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

And for the partition function, you can just do this (say you wanted all pairs lower then 4): 对于partition功能,您可以这样做(假设您希望所有对都低于4):

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

Which gives (all lower then 4 on the left, all greater on the right): 给出(左边全部低于4,右边全部更高):

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

Just to share with you: this works! 只是与您分享:这有效! Thanks alot to all people who helped me, you're all great! 非常感谢所有帮助过我的人,你们都很棒!

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))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到类型^的证据参数的隐含值 - could not find implicit value for evidence parameter of type ^ 无法找到类型证据参数的隐式值 - Could not find implicit value for evidence parameter of type 找不到类型为证据的隐式值-Spark - could not find implicit value for evidence parameter of type - Spark 使用 scalamock:找不到类型错误的证据参数的隐式值 - Using scalamock: Could not find implicit value for evidence parameter of type error Scala编译错误-找不到类型为证据的隐式值 - Scala compile error - could not find implicit value for evidence parameter of type 找不到scalaz.Applicative类型的证据参数的隐含值 - could not find implicit value for evidence parameter of type scalaz.Applicative 找不到scala.reflect.ClassManifest [T]类型的证据参数的隐式值 - Could not find implicit value for evidence parameter of type scala.reflect.ClassManifest[T] Flink - 导入类型不能修复“无法找到类型…TypeInformation 的证据参数的隐式值” - Flink - importing types doesn't fix “could not find implicit value for evidence parameter of type …TypeInformation” 找不到隐式json格式的证据参数的隐式值 - could not find implicit value for evidence parameter for implicit json formats 找不到JsonSupport.this.JF类型的证据参数的隐式值[org.joda.time.LocalDateTime] - could not find implicit value for evidence parameter of type JsonSupport.this.JF[org.joda.time.LocalDateTime]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM