命名零件:
coords :: Event t [Coord]
ticks :: Event t ()
如果我们想记住最近Coord的直到下一次触发ticks,那么我们必须在 some monadReflex m中。这是允许瞬态Event持久化的 monad。
您要记住的核心内容是一堆Coord. 让我们试试这个:
data Stack a = CS {
cs_lastPop :: Maybe a
, cs_stack :: [a]
} deriving (Show)
stack0 = CS Nothing []
pop :: Stack a -> Stack a
pop (CS _ [] ) = CS Nothing []
pop (CS _ (x:xs)) = CS (Just x) xs
reset :: [a] -> Stack a -> Stack a
reset cs (CS l _) = CS l cs
那里还没有任何反应,两个功能可以Stack Coord按照您在问题中提到的方式进行调整。
驱动它的反射代码将Dynamic t (Stack Coord)通过指定它的初始状态和所有修改它的东西来构建一个:
coordStack <- foldDyn ($) stack0 (leftmost [
reset <$> coords
, pop <$ ticks
])
这里leftmost需要一个Stack Coord -> Stack Coord函数列表,这些函数依次应用于stack0by foldDyn ($) (只要coords并且ticks永远不会出现在同一帧中)。
驾驶这一切main:
main :: IO ()
main = mainWidget $ do
t0 <- liftIO getCurrentTime
-- Some make up 'coords' data, pretending (Coord ~ Char)
coordTimes <- tickLossy 2.5 t0
coords <- zipListWithEvent (\c _ -> c) ["greg","TOAST"] coordTimes
ticks <- tickLossy 1 t0
coordStack <- foldDyn ($) stack0 (leftmost [
reset <$> coords
, pop <$ ticks
])
display coordStack