Haskell:FRP 反应秒差距?
Haskell 中是否有(或者可能有)反应式 Parsec(或任何其他纯函数解析器)?
简而言之,我想自己逐个字符地向解析器提供数据,并获得与我提供的足够多的结果以获得输出一样多的结果。
或者更简单,我如何在 foldr
或至少 map
中做到这一点?
我们是否需要它们的不同版本来支持这种反应行为?
编辑
我的问题特别是关于玻璃钢的。我使用解析器作为示例,这是我能想到的最好的方法来澄清我的问题并给出我需要的大图。
我相信FRP不仅仅是UI,对吧?
Is there (or is it possible to have) a reactive Parsec
(or any other pure functional parser) in Haskell?
Simply put, I want to feed parser myself char by char and get results as much as I feed enough to get output.
Or much simpler, how can I do it in foldr
or at least map
?
Do we need a different version of them to support such reactive behavior?
EDIT
My question is about FRP in particular. I used a parser as an example, which was the best I could think of to clarify my question and give big picture of what I need.
I believe FRP is not just about UI, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法在 Parsec 中进行在线解析,它必须消耗所有输入才能确定是否存在有效的解析。
不过,还有其他选择。
一种可能性是使用 Utrecht 解析器组合器,它具有在线解析功能。
You cannot do online parsing in Parsec, it has to consume all the input in order to determine whether there is a valid parse or not.
However there are alternatives.
One possiblity is to use the Utrecht parser combinators, it has online parsing among its features.
我认为称其为“FRP”是不对的,这种东西的正确名称是 在线算法,这意味着解析器一收到输入就产生输出。 (与离线算法相反,离线算法中解析器预先接收整个输入并从中生成输出。)
在 Haskell 中,惰性求值使得编写在线算法变得容易。 Malcom Wallace 开发了一套特殊的解析器组合器,用于使用惰性求值的在线解析。
I don't think it's right to call this "FRP", the right name for this kind of thing is online algorithm, which means that the parser produces output as soon as it receives input. (As opposed to offline algorithm, where the parser receives the entire input upfront and produces an output from that.)
In Haskell, lazy evaluation makes it easy to write online algorithms. Malcom Wallace has developed a special set of parser combinators for online parsing that use lazy evaluation.
您可以用秒差距进行在线解析,但要做到这一点,您需要将其分层在迭代器之类的东西之上。
Parsec 3 能够处理任意 Stream 类型,因此您可以创建一个 Stream 实例,将当前“流”视为一个位置,并使用 iteratee 检索该位置处的值。
一个这样的例子是 iteratee-parsec 包。
另一种方法是由 parsing trifecta talk on iteratees 和parsec(警告 PDF):
一种折衷方案是构建一个类似迭代器的类型,该类型缓冲最后几个块片段而不是全部片段,以使其能够保留有限的空间利用率,并且依靠 iteratee 机制进行回溯。这是我目前使用的,但我在网上没有任何代码。
一旦您通过在 Iteratee 上运行 Parsec 来实现反向控制,就可以很容易地一次输入一个字符并查看它是否已成功识别任何内容。
You can do online parsing in Parsec, but to do so you need to layer it on top of something like an iteratee.
Parsec 3 is capable of working with arbitrary Stream types, so you can make an instance of Stream which views the current 'stream' as a position, and uses an iteratee to retrieve the value at that position.
One such example is the iteratee-parsec package.
Another approach is provided by parsing trifecta talk on iteratees and parsec (warning PDF):
A compromise is to build an iteratee-like type that buffers the last few chunk fragments rather than all of them to enable it to retain bounded space utilization, and rely on the iteratee machinery for backtracking beyond that. This is what I currently use, but I don't have any code online for it.
Once you have inverted control by running Parsec on top of an Iteratee, it is quite easy to feed it input a character at a time and see if it has managed to recognize anything yet.
看看 attoparsec-conduit,使用正确的解析器,它可以是将字节流转换为解析数据结构流的有用方法
Have a look at attoparsec-conduit, with the correct parser it can be a useful way to translate a stream of bytes into a stream of parsed data structures