English 中文(简体)
为什么如此大 仅归还单一项目?
原标题:Why does maxBy return only single item?
最佳回答

没有单一的指挥。 我最简短地知道——将组别everything,而不仅仅是作为中间标准的最高值

xs.groupBy(f).maxBy(_._1)._2

为了提高效率,文件夹是寻找钱财和最大程度以及各种类似事物的良好通用工具。 基本上,在积累一些答案时,你需要掌握你的收集工作。 在这种情况下,

(xs.head /: xs.tail) {
  (biggest, next) => if (f(biggest) < f(next)) next else biggest
}

如果您不考虑对每项内容的两次重新评价职能,则将履行<条码>最大限度(f)<>。

((xs.head, f(xs.head)) /: xs.tail) {
  case (scored, next) =>
    val nextscore = f(next)
    if (scored._2 < nextscore) (next, nextscore)
    else scored
}._1

只进行一次评价。 如果你想保持顺序,你可以修改顺序。

(Seq(xs.head) /: xs.tail) {
  (bigs, next) =>
    if (f(bigs.head) > f(next)) bigs
    else if (f(bigs.head) < f(next)) Seq(next)
    else bigs :+ next
}

保留名单(相应的单一评价表留待读者使用)。

最后,如果你再次愿意使用一些可变的变量的话,甚至接近最高效率的版本都是的。

val result = {
  var bigs = xs.take(0).toList
  var bestSoFar = f(xs.head)
  xs.foreach{ x =>
    if (bigs.isEmpty) bigs = x :: bigs
    else {
      val fx = f(x)
      if (fx > bestSoFar) {
        bestSoFar = fx
        bigs = List(x)
      }
      else if (fx == bestSoFar) bigs = x :: bigs
    }
  }
  bigs
}

(这种情况将按相反的顺序返回。)

问题回答

如果是,

case class Student(name: String, grade: String)
val students = List(Student("Mike", "A"), Student("Pete", "B"), Student("Paul", "A"))

因此,这是一种非常简单的O(N)办法,它并不涉及建立任何中间名单:

val bestGrade = students.minBy(_.grade).grade
students.filter(_.grade == bestGrade)    //List(Student(Mike,A), Student(Paul,A))

我们在此使用<条码>minBy,因为指令了各项指令。

作为一种方法:

def multiMinBy[A,B](xs: Traversable[A])(f: A => B)(implicit ord: Ordering[B]) = {
  val minVal = f(xs minBy f)
  xs filter (f(_) == minVal)
}

scala> multiMinBy(students)(_.grade)
res26: Traversable[Student] = List(Student(Mike,A), Student(Paul,A))

我知道的标准图书馆没有职能。

maxBy  :: (a -> a -> Ordering) -> [a] -> [a]
maxBy  _ [] = undefined
maxBy  f (x:xs) = foldr step [x] xs
  where step y acc@(z:_) = case f y z of
          GT -> [y]
          EQ -> y:acc
          LT -> acc

[编辑] 谁,这是一个问题:

Translated to Scala, given a list xs and a comparator compare:

(List(xs.head) /: xs.tail) { (acc, y) =>
  y compare acc.head match {
    case 1  => List(y)
    case 0  => y :: acc
    case -1 => acc
  }
}

学生和学生名单:

class Student (val name: String, val grade: String) {
  override def toString = grade + "::" + name
}
val students = List (new Student ("Mike", "A"), new Student ("Pete", "B"), new Student ("Paul", "A"))

功能性、保证性的解决办法,在T组清单中实现分化,以及比较2 Ts的方法:

// ext: extreme, o: other, s:sample(student)
@tailrec
def collectExtreme [T](l: List[T], ext: ((T, T) => Int), carry: List[T]=List.empty) : List[T] =
  l match {
    case Nil => carry
    case s :: xs => carry match {
      case Nil => collectExtreme (xs, ext, List (s))
      case o :: _ => ext (s, o) match {
        case 0 => collectExtreme (xs, ext, s :: carry)
        case -1=> collectExtreme (xs, ext, l)
        case 1 => collectExtreme (xs, ext, carry)
      }
    }
  }
def cmp (s: Student, o: Student): Int = s.grade(0) - o.grade(0) 

collectExtreme (students, cmp) 

收集工作也只剩下1次。





相关问题
Is HashMap in Java collision safe

I am developing a parser that needs to put key value pairs in hashmap. A key can have multiple values which I can do in this way HashMap<String,ArrayList<String>> . What happens if the ...

iterating over map and array simultaneously in a for loop

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop. I have ...

PLSQL Collections - how to use table of records?

I m new to PL/SQL and I m trying to use a table of records, but I don t know how to use this feature. What is the problem? DECLARE TYPE TIP IS RECORD ( F1 ...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...