1

从使用法线Int保持计数器状态的 Reactive Banana Wx 中的 Counter 示例开始:

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

如何Int用更通用的方式替换和更新计数器data

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

任何想法?

4

1 回答 1

3

的类型accumB是:

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

因此,如果你想Behavior t Counter用它定义 a ,你需要使用带有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) 
于 2015-09-13T17:37:50.873 回答