是否有可以像自动机一样使用的单子?

发布于 2024-12-20 07:25:01 字数 520 浏览 1 评论 0原文

我正在编写一个从某些输入数据类型到输出数据类型的流转换器。输入是由用户进行的,因此事件之间有一些时间间隔。因为每个输入都需要加载一些资源,所以我想“展望未来”,即将所有可能的输入发送到主计算并根据结果预加载资源。

目前,每次输入后总是有一个输出,但改变这一点最终可能会变得有趣。

我通过 Ross Paterson 的自动机变压器成功地实现了这一点。我不确定我的解决方案是否最佳。

  • 有很好的示例如何做到这一点吗?也许甚至有测试代码?
  • 也可以用 monad 来实现吗? (示例?解释为什么这是不可能的?)

编辑: 在要求更多细节后,我在此处添加了代码。现在我将其删除(这是无法理解的)并添加一些其他解释。我的问题得到了解答。

我的目的是让主事件循环在每个用户输入被馈送到箭头/流转换器/其他内容之后停止。然后它会存储当前的自动机状态,并将所有可能的输入(假事件)一一发送到自动机,并查看必须加载哪些资源,以缓存它们。在下一个真实事件发生后,它将使用缓存以获得更好的响应能力。主要计算不应受此影响。

I'm writing a stream transformer from some Input data type to an Output data type. Input is made by the user, so there is some time between the events. Because each input requires some resource loading, I'd like to "look into the future", i.e. send all possible inputs to the main computation and preload resources based on the results.

Currently, there is always exactly one output after each input but it might eventually become interesting to change this.

I succeeded to implement that with the Automaton transformer by Ross Paterson. I'm not sure my solution is optimal.

  • Are there nice examples how to do this? Perhaps even with test code?
  • Can it be achieved with a monad as well? (Examples?, Explanation why it's impossible?)

Edit:
After the call for more specifics, I added code here. Now I'm removing it (it was not understandable) and add some other explanation. My question is answered thaugh.

My intention was to have the main event loop stop after each user input that has been fed to the arrow/stream transformer/whatever. Then it would store the current automaton state and send all possible inputs (fake events) one by one to the automaton and see what resources must be loaded, to cache them. After the next real event, it would use the cache for better responsiveness. The main computation should not be influenced by this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-12-27 07:25:01

Netwire 库涵盖了您提到的所有用例。它将罗斯的自动箭头推广到一系列线箭头。我还没有完成 wiki 页面,但它应该足以让您开始。

将其与某些单子 m 的 Kleisli (LogicT m) 结合起来,您将得到不确定的连线。

附加说明:您想要的不是 monad。

All the use cases you mentioned are covered by the Netwire library. It provides a generalization of Ross' automaton arrow to a family of wire arrows. I haven't finished the wiki page yet, but it should give you enough to start.

Combining this with Kleisli (LogicT m) for some monad m you get nondeterministic wires.

And as an additional note: What you want is not a monad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文