2012-12-15 33 views
4

我现在实际上被阻止了大约4个小时。我想要得到一个由int值排序的成对List [String,Int]。功能partiotion工作正常,所以应该bestN,但是当加载到我的解释器时,我得到:找不到类型的证据参数的隐式值Ordered [T]

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

编辑:第一个功能通过应用谓词每个成员把一个表分为二,bestN函数返回一个列表中最低的n个成员列表。和类是有做对的可比性,在这种情况下,我想你做的是:

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

这个给定的名单我想例如使用:

bestN(z, 3) 

结果:

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

回答

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 
) 
+0

好了,有了这个代码,我得到一个奇怪的编译错误: '发现:(U,V)' '要求:(U,V)' '高清CMP(A:(U,V) ,b:(U,V))=((a:OrderedPair [U,V])<(b:OrderedPair [U,V]))' – Theolodis

+0

好吧,得到了错误:) – Theolodis

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,全部大于右边):

​​
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)) 
} 
+0

如果Gulliver - > 1?或者阿尔弗雷德,彼得,Xaver的最佳3人? –

相关问题