我有以下测试程序:
{-# LANGUAGE RankNTypes #-}
module Main where
import Reactive.Banana
import Reactive.Banana.Frameworks
import Reactive.Banana.Switch
import Data.IORef
import Control.Monad
main = do
fShowRef <- newIORef (const (return ()))
fNumRef <- newIORef (const (return ()))
let
netDesc :: forall t. Frameworks t => Moment t ()
netDesc = do
(eNum, fNum) <- newEvent
(eShow, fShow) <- newEvent
liftIO (writeIORef fShowRef fShow)
liftIO (writeIORef fNumRef fNum)
let beh = stepper 0 eNum
behTrimmed <- trimB beh
reactimate (printInitial behTrimmed <$ eShow)
actuate =<< compile netDesc
fNum <- readIORef fNumRef
fNum 1
fShow <- readIORef fShowRef
fShow ()
printInitial :: AnyMoment Behavior Int -> IO ()
printInitial beh = do
let
netDesc :: forall t. Frameworks t => Moment t ()
netDesc = do
behNow <- now beh
initVal <- initial behNow
liftIO (print initVal)
-- compiling is enough, I just want to see the initial value
void (compile netDesc)
我希望这能打印出来
1
但它打印
0
我正在创建一个包含两个事件的网络。eNum
是Int
s中的eShow
一个事件()
。每当eShow
被提出,printInitial
被执行。
printInitial
打印行为的初始值trimB (stepper 0 eNum)
。我认为trimB
随后now
会在编译包含它的网络描述之前丢弃历史记录。这意味着每次eShow
引发时,都会出现一个新的行为,该行为以当时的值开始beh
。显然,事实并非如此。
所以,
- 目的是
trimB
什么? - 是否可以转换
x :: Behavior t0 a
为aty :: Behavior t1 a
的initial y
值?x
t1