Reactive Banana:更改数据状态

Reactive Banana: Change status in data

从 Reactive Banana Wx 中的计数器示例开始,它使用正常 Int 来保持计数器状态:

let networkDescription :: forall t. Frameworks t => Moment t () 
    networkDescription = do
    eup   <- event0 bup   command
    edown <- event0 bdown command

    let
        counter :: Behavior t Int
        counter = accumB 0 $ ((+1) <$ eup) `union` (subtract 1 <$ edown)

    sink output [text :== show <$> counter]

network <- compile networkDescription
actuate network

如何用更通用的 data 替换和更新 Int 计数器,例如:

data Counter = Counter {
     count :: Int
 } deriving (Show)

let
    counter :: Behavior t Counter
    counter = accumB Counter { count = 0 } $ ??????

sink output [text :== show <$> count counter]

我不知道如何用这样的东西来引用内部 count 函数:

count = count mycounter + 1

有什么想法吗?

accumB的类型是:

accumB :: a -> Event t (a -> a) -> Behavior t a

所以如果你想用它定义一个Behavior t Counter你需要使用带有Counter -> Counter函数的事件:

-- For the sake of convenience...
overCount :: (Int -> Int) -> Counter -> Counter
overCount f c = c { count = f (count c) }
counter = accumB Counter { count = 0 } $
    (overCount (+1) <$ eup) `union` (overCount (subtract 1) <$ edown)