【问题标题】:Reactive Banana: Change status in dataReactive Banana:数据中的更改状态
【发布时间】:2015-09-13 16:41:34
【问题描述】:

从使用普通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

如何使用更通用的 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

有什么想法吗?

【问题讨论】:

    标签: haskell reactive-banana


    【解决方案1】:

    accumB 的类型是:

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

    所以如果你想用它定义一个Behavior t Counter,你需要使用带有Counter -&gt; 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) 
    

    【讨论】:

      猜你喜欢
      • 2013-02-16
      • 2012-06-03
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多