`Akka-Streams` 中 `extrapolate` 的用例是什么?

What is the Use Case of `extrapolate` in `Akka-Streams`?

我刚刚在 akka-streams 中尝试了 conflateextrapolate

由于 conflate 对我来说完全有意义,所以我不明白 extrapolate 的用例。

为什么我们要为下游增加更多的工作——当上游不需要它时?

来自 Scala 文档:

Allows a faster downstream to progress independent of a slower upstream.

举个例子:

游戏开发

在视频游戏中,通常至少有两个 "loops":一个 logic/game 循环和一个渲染循环。通常,游戏循环的速率("tick rate")比渲染循环的速率("frame rate")慢。例如,逻辑节拍可能每秒出现 10 次,但帧速率通常应至少为每秒 60 帧。为了在刻度之间呈现某些内容,游戏开发人员使用 extrapolation or interpolation。您可能已经猜到,extrapolate 函数非常适合外推。下面是一个滴答率为每秒 10 滴答且没有帧速率限制的示例:

Source.tick(0.millis, 100.millis, 0)
    .scan(intialGameState) { (g, _) => tick(g) }
    .extrapolate(extrapolateFn)
    .runForeach(render)

现在extrapolateFn只需要return一个迭代器来提供按需推断的游戏状态:

def extrapolateFn(g: GameState) = Iterator.continually {
  // Compute how long it has been since `g` was created
  // Advance the state by that amount of time
  // Return the new state
}