加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 找不到有序类型的证据参数的隐含值[T]

发布时间:2020-12-16 08:48:16 所属栏目:安全 来源:网络整理
导读:我实际上已经被封锁了大约4个小时了.我想得到一个按其int值排序的对象列表[String,Int].函数partiotion工作正常,所以应该是bestN,但是当把它加载到我的解释器中时,我得到: console:15: error: could not find implicit value for evidence parameter of typ
我实际上已经被封锁了大约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",Pair("Ulf",2),Pair("Alfons",6),Pair("Gulliver",3))

有了这个给定的List我想得到例如:

bestN(z,3)

结果:

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

解决方法

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

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

package evident

object Test extends App {

  def partition[T](pred: (T)=>Boolean,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,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",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,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,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 ","}")
      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
  )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读