minBy / maxBy 函数在出现平局时会发生什么?

huangapple go评论67阅读模式
英文:

What happens on a tie on minBy / maxBy functions?

问题

If you run a minBy or maxBy from the kotlin-stdlib on a collection with two objects and there's a tie for the lowest / highest value, which one will the function return?

英文:

If you run a minBy or maxBy from the kotlin-stdlib on a collection with two objects and there's a tie for the lowest / highest value, which one will the function return?

答案1

得分: 5

以下是翻译好的部分:

  1. "The documentation of both minBy and maxBy state:" 翻译为 "关于minBymaxBy的文档中提到:"
  2. "Returns the first entry yielding the (smallest / largest) value ..." 翻译为 "返回产生(最小/最大)值的第一个条目..."
  3. "The behaviour is clearly defined and part of the API contract, and thus can be relied upon." 翻译为 "行为已明确定义并包含在API合同中,因此可以依赖它。"
  4. "You have run your own experiments to come to the same conclusion. A word of caution though:" 翻译为 "您已经进行了自己的实验,得出了相同的结论。但请注意:"
  5. "While running experiments to determine the behaviour of third party code can work, you should be aware, that the result from an experiment like this is far from being a proof." 翻译为 "虽然运行实验以确定第三方代码的行为可能有效,但您应该意识到,这种实验的结果远非证据。"
  6. "Assuming the behaviour was not defined in the API contract, a new release of the third party library might change this behaviour at any time." 翻译为 "假设行为未在API合同中定义,第三方库的新版本可能随时更改此行为。"
  7. "Additionally, there might be edge cases, in which the result differs from the observation your experiment yielded, just because it has not run into this edge case." 翻译为 "此外,可能存在边缘情况,导致实验观察的结果与之不同,只是因为它尚未遇到这种边缘情况。"
  8. "A stupid but simplified example, which your experiments most likely wouldn't have observed the different behaviour:" 翻译为 "一个愚蠢但简化的例子,您的实验很可能没有观察到不同的行为:"
  9. "If a behaviour is not specified in the API contract but you still want to rely on it, I'd rather analyze the source code, if it's available, to ensure a function works as expected, to avoid pitfalls like aforementioned." 翻译为 "如果API合同中未指定行为但您仍然希望依赖它,我建议分析源代码(如果可用),以确保函数按预期工作,以避免上述提到的陷阱。"
  10. "In general I'd recommend to not rely on behaviour, that's not clearly defined in an API contract." 翻译为 "总的来说,我建议不要依赖未在API合同中明确定义的行为。"
  11. "1: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/min-by.html" 翻译为 "1: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/min-by.html"
  12. "2: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max-by.html" 翻译为 "2: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/max-by.html"
英文:

The documentation of both minBy and maxBy state:

> Returns the first entry yielding the (smallest / largest) value ...

The behaviour is clearly defined and part of the API contract, and thus can be relied upon.


You have run your own experiments to come to the same conclusion. A word of caution though:

While running experiments to determine the behaviour of third party code can work, you should be aware, that the result from an experiment like this is far from being a proof.
Assuming the behaviour was not defined in the API contract, a new release of the third party library might change this behaviour at any time.

Additionally, there might be edge cases, in which the result differs from the observation your experiment yielded, just because it has not run into this edge case.
A stupid but simplified example, which your experiments most likely wouldn't have observed the different behaviour:

fun <T, R : Comparable<R>> Iterable<T>.minBy(
    selector: (T) -> R
): T {
   val returnLastMatch = random.nextInt() % 1_000_000 == 0

   ...

   if(returnLastMatch) {
      lastMatch
   } else firstMatch
}

If a behaviour is not specified in the API contract but you still want to rely on it, I'd rather analyze the source code, if it's available, to ensure a function works as expected, to avoid pitfalls like aforementioned.

In general I'd recommend to not rely on behaviour, that's not clearly defined in an API contract. Luckily for you, the behaviour of both minBy and maxBy is clearly defined regarding your case.

答案2

得分: -2

以下是要翻译的内容:

"Of the tied objects, it will return the first one it encounters in the list. See this Kotlin playground for to see how it works: https://pl.kotl.in/aapJRHy0Q"

英文:

Of the tied objects, it will return the first one it encounters in the list. See this Kotlin playground for to see how it works: https://pl.kotl.in/aapJRHy0Q

huangapple
  • 本文由 发表于 2023年5月7日 05:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定