我想在 Behavior 上映射一个 IO 操作,但 (threepenny-gui) 中没有这样的函数 是否有某种方法可以使用公开的 API 构建它。它在语义上合理吗?
我已经在三便士内实现了这个,在我的用例中工作正常。
-- Just bind the IO action to the Latch
unsafeMapIOB :: (a → IO b) → Behavior a → Behavior b
unsafeMapIOB f (B l e) = B (Prim.bindL (Prim.Latch ∘ f) l) e
-- Wrap the IO bind
bindL :: (a -> Latch b) -> Latch a -> Latch b
bindL f l = Latch { readL = (readL ∘ f) =<< readL l}
-- Map the IO action over both Behavior and Event
unsafeMapIOT :: (a -> IO b) -> Tidings a -> Tidings b
unsafeMapIOT f x = tidings (unsafeMapIOB f $ facts x) (unsafeMapIO f $ rumors x)
我从来没有单独使用 unsafeMapIOB 只是 unsafeMapIOT 。我相信正在发生的是 unsafeMapIOB 只执行一次,然后 unsafeMapIO 被事件触发。