Data.Map - 为什么有 `takeWhileAntitone` 而没有 `takeWhile`?

Data.Map - why is there `takeWhileAntitone` but no `takeWhile`?

我对 Data.Map API 感到困惑。我正在寻找一种以 log(n) 成本查找地图键范围的简单方法。这是一个称为“二进制搜索”的基本概念,也可能是“平分”。

我看到这个奇怪的 takeWhileAntitone 函数,我需要在其中提供一个“antitone”谓词函数。第一次接触这个概念

阅读有关该主题的维基百科后,这似乎只是在说当按键顺序应用于参数时,函数可能只有一个地方从 True 变为 False。这符合二进制搜索的要求。

由于 API 是用一种奇怪的(对我来说)语言记录的,所以我想在这里问一下:

Since the API is documented in a strange (to me) language, I wanted to ask here:

  • if my understanding is correct, and

是的。 takeWhileAntitone(以及库中其他类似命名的变体)是对键进行二进制搜索的函数。它没有命名为 takeWhile,因为它不适用于任何参数谓词,因此如果您正在审查代码,它可以提醒您进行检查。

  • is there a reason these functions aren't called bisect, binarySearch or similar?
  1. 此名称用于区分变体 takeWhileAntitonedropWhileAntitonespanAntitone“进行二分查找”但最终结果不同。

  2. takeWhile 是来自 Haskell 标准库(在 Data.List 中)的众所周知的名称。

  3. 在 FP 中,我们喜欢区分“什么”和“如何”。 “二进制搜索”是一种算法(“如何”)。 “take while”字面上也是“how”,但它的含义可以说更自然地与“what”(满足谓词的元素的最长前缀)相关联。特别是,将“take while”解释为“最长前缀”不依赖于关于谓词的任何假设。