考虑以下代码:
-- this defines what our 'state' will be
data Direction = North | East | South | West deriving (Eq, Show, Enum)
data State = State Int Bool Direction deriving (Show)
initialState :: State
initialState = State 0 True North
-- a simple routine to change a state and count the number of
-- changes
nextState :: State -> State
nextState (State i _ West) = State (i+1) False South
nextState (State i _ North) = State (i+1) True East
nextState (State i b s) = State i b $ (if b then succ else pred) s
-- a wire with local state
stateWire :: Wire s () m a State
stateWire = stateWireFrom initialState
where
stateWireFrom s = mkSFN $ \_ -> (nextState s, stateWireFrom (nextState s))
-- let's run the wire!
main = testWire clockSession_ stateWire 正如您可以想象的那样,testWire将尽可能快地运行线路,并将输出打印到屏幕上。但如果我想每2秒运行一次我的电线呢?从文档来看,periodic可能是解决方案:
-- Since periodic generates events, asSoonAs is used to 'unwrap' the Event
main = testWire clockSession_ (asSoonAs . periodic 2 . stateWire)这个几乎可以使用。输出似乎是静态的,大约2秒,但是当它被更新时,很明显,当输出停止时,电线正在运行。也许我该换个角度?
-- Now, this does make more sense to me...
main = testWire clockSession_ (stateWire . periodic 2)然而,最终的结果和我的第一次尝试完全一样。我在这里错过了什么?
编辑:参见这个答案,以获得接受的答案的(次等)选项。
发布于 2015-03-02 21:58:07
问题似乎在于,您将您的stateWire视为一个连续的连线,但它本身确实应该是一个事件线。假设我正确理解了您的意图,它可能应该是accumE (flip $ const nextState) initialState -参见事件文档 for accumE -然后您可以这样使用它:
stateWire . periodic 2 (相反的方法不起作用)。
您的原始版本不能工作的原因是,当没有事件时,periodic不会抑制,而是只生成一个NoEvent值。由于您的stateWire只是忽略它的输入,当周期线在前面时,事件是否产生对它没有任何影响,而将周期线放在后面仅仅意味着“周期性地捕捉当前状态的快照”,这也不是您想要的。
注意:前一段中的“前端”和“后面”指的是执行的顺序,而不是源代码中的布局,如果使用.组合器,则布局会相反。
发布于 2015-03-03 18:29:29
作为可接受的答案的另一种选择,也可以过滤掉NoEvent,而无需更改线路:
main = testWire clockSession_ (stateWire . when occurred . periodic 2)在这种情况下,电线将改变状态,抑制2秒,然后再改变它。
另一个(接受)答案的工作原理不同:连线将改变状态,然后继续产生相同的结果2秒,然后再次更改它。
https://stackoverflow.com/questions/28818935
复制相似问题