FRP(反应式):如何使用filterE?
我预计接下来会在一秒钟内打印“()”10次。但一秒钟后它就挂起了。为什么?
adaptE $ fmap print $ filterE (const True) $ atTimes [0.1, 0.2 ..]
我发现它与filterE中使用的liftM有关:
filterE :: (Ord t, Bounded t) => (a -> Bool) -> EventG t a -> EventG t a
filterE p m = justE (liftM f m)
where
f a | p a = Just a
| otherwise = Nothing
我尝试使用fmap
重新实现filterE
,它似乎有效。为什么? 标准 filterE
是如何设计使用的?
我发现自己重新实现了 reactive
包提供的许多标准函数(例如 diffE
、integrate
)。这是否意味着该软件包有错误或者我以错误的方式使用它?
谢谢!
I expect the next will print "()" 10 times in a second. But it hangs after a second. Why?
adaptE $ fmap print $ filterE (const True) $ atTimes [0.1, 0.2 ..]
I found that it is related to liftM used in filterE:
filterE :: (Ord t, Bounded t) => (a -> Bool) -> EventG t a -> EventG t a
filterE p m = justE (liftM f m)
where
f a | p a = Just a
| otherwise = Nothing
I tried to reimplement filterE
using fmap
and it seems to work. Why?
How the standard filterE
is designed to be used?
I found myself reimplementing a lot of standard functions provided by the reactive
package (e.g. diffE
, integrate
). Does it mean that the package is buggy or I use it in a wrong way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的经验,反应式是有问题的,特别是对于
Event
的Monad
实例(monadjoin
操作有点过于严格,我们不太清楚为什么)。如果可能的话,避免这种情况。响应式是一项实验,代表了通过更多运行时支持可能实现的目标。请参阅 Yampa,以获得更稳定、更可靠、更广泛使用的 FRP 库,即使它的表现力稍差一些。In my experience reactive is buggy, especially with regard to the
Monad
instance ofEvent
(the monadjoin
operation is slightly too strict and we're not exactly sure why). Avoid that if possible. Reactive was an experiment, and represents what might be possible with more runtime support. See Yampa for a more stable, reliable, and well-traveled FRP library, even if it is a bit less expressive.