我想用一个依赖于旧状态的函数修改我的状态,但也引入了一些随机性。我的函数f
如下所示:
f :: State -> Eff (random :: RANDOM) State
我想我的状态应该是纯粹的,除了使用Eff
之外我不知道如何摆脱unsafePerformEff
,所以我这样做了:
eval :: Query ~> H.ComponentDSL State Query g
eval (Tick next) = do
H.modify (unsafePerformEff <<< f)
pure next
这有效,但必须有另一种更安全的方法。我已经将随机效果添加到我的主函数中:
main :: Eff (H.HalogenEffects (random :: RANDOM)) Unit
但是eval
应该怎么样?也许modify
在这里不起作用,还有另一种更新状态的方法吗?
Purescript Halogen, side effect (random number)对我不起作用,因为f
取决于旧状态。
答案 0 :(得分:1)
modify
本身不允许您执行有效的更新,但是,您可以使用get
然后使用modify
(或set
)来执行此操作。改编自随机的另一个例子:
module Main where
import Prelude
import Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Random (randomInt, RANDOM)
import Halogen as H
import Halogen.HTML.Events.Indexed as HE
import Halogen.HTML.Indexed as HH
import Halogen.Util (runHalogenAff, awaitBody)
type State = { n :: Int }
initialState :: State
initialState = { n: 3 }
data Query a = NewRandom a
ui :: forall eff. H.Component { n :: Int } Query (Aff (random :: RANDOM | eff))
ui =
H.component { render, eval }
where
render :: State -> H.ComponentHTML Query
render state =
HH.button
[ HE.onClick $ HE.input_ NewRandom ]
[ HH.text $ show state.n ]
eval :: Query ~> H.ComponentDSL State Query (Aff (random :: RANDOM | eff))
eval (NewRandom next) = do
state <- H.get
nextState <- H.fromEff (nextRandom state)
H.set nextState
pure next
nextRandom :: State -> Eff (random :: RANDOM | eff) State
nextRandom { n } = do
nextN <- randomInt (n + 1) (n + 10)
pure { n: nextN }
main :: forall eff. Eff (H.HalogenEffects (random :: RANDOM | eff)) Unit
main =
runHalogenAff do
body <- awaitBody
H.runUI ui initialState body
可能导致类型错误的是这种类型的签名:
f :: State -> Eff (random :: RANDOM) State
效果行已关闭,这意味着它不会与任何其他行统一,您可能需要这样:
f :: forall eff. State -> Eff (random :: RANDOM | eff) State