2

我一直在寻找一段时间,我认为我误解了关于切换的一些基本内容。考虑以下代码

mywire :: HasTime t s => Wire s () IO a Int
mywire = pure 10

mycont :: HasTime t s => Wire s () IO a (Wire s () IO a Int)
mycont = after 10 . pure (pure 20)

mycont' :: HasTime t s => Wire s () IO a (Event (Wire s () IO a Int))
mycont' = now . mycont

mything :: HasTime t s => Wire s () IO a (Int, Event (Wire s () IO a Int))
mything = (,) <$> mywire <*> mycont'

mainwire :: (Monad m, HasTime t s) => Wire s () m a Int
mainwire = switch mything

main :: IO ()
main = testWire clockSession_ mainwire

请注意,我在这里更加冗长只是为了了解所涉及的某些类型。我期望输出的是数字 10 重复 10 秒,然后我期望mainwire切换到从返回的事件mything(因为事件从mything延迟 10 秒。)我看到的是主线抑制 10 秒,然后是 20。为什么我在切换之前没有看到最初输出 10?我想我误解了切换应该如何工作,任何澄清将不胜感激。谢谢你。

4

1 回答 1

3

问题出在now . mycont. now直接将anya变成a Event a,从而switch直接切换到mycontwire。此线禁止 10 秒,然后输出20。要达到您想要的效果,您可以使用以下at功能:

import Prelude hiding ((.))
import Control.Wire

mywire :: (Monad m, HasTime t s) => Wire s () m a Int
mywire = pure 10

mycont :: (Monad m, HasTime t s) => Wire s () m a (Event (Wire s () m a Int))
mycont = at 10 . pure (pure 20)

mything :: (Monad m, HasTime t s) => Wire s () m a (Int, Event (Wire s () m a Int))
mything = (,) <$> mywire <*> mycont

mainwire :: (Monad m, HasTime t s) => Wire s () m a Int
mainwire = switch mything

main :: IO ()
main = testWire clockSession_ mainwire
于 2015-10-24T08:57:21.170 回答