Purescript 卤素,副作用(随机数)
Purescript Halogen, side effect (random number)
在 PureScript Halogen 项目中,我想将状态设置为随机数,但如何提取值?正常
r <- randomInt 1 10
在 eval 函数中时不编译。
module Main where
import Prelude
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 e. H.Component { n :: Int } Query e
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 e
eval (NewRandom next) = do
H.modify (\state -> state { n=12 } )
--I'd like to set n to a random number
--but I don't know how.
--let r = randomInt 1 10
--H.modify (\state -> state { n=r } )
pure next
main :: Eff (H.HalogenEffects ()) Unit
main =
runHalogenAff do
body <- awaitBody
H.runUI ui initialState body
你需要在你的 ComponentDSL
中使用合适的 monad(你目前有 e
类型的 var)才能实现,然后你可以使用 H.fromEff
提升randomInt
:
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
r <- H.fromEff $ randomInt 1 10
H.modify (\state -> state { n=r } )
pure next
main :: forall eff. Eff (H.HalogenEffects (random :: RANDOM | eff)) Unit
main =
runHalogenAff do
body <- awaitBody
H.runUI ui initialState body
(旁白:如果你正在做有效的事情,即使你只需要 Eff
,最简单的方法是使用 Aff
作为 ComponentDSL
monad,就像你使用 runUI
它期望它是 Aff
- 可以在 Halogen.Component
模块中使用 interpret
来更改 monad,但是因为你只是使用 interpret liftAff
反正你也可以直接上Aff
.)
查看 "Non-state effects" section of the guide, or the AJAX example 以了解有关 eval
中 运行 效果的更多详细信息。
在 PureScript Halogen 项目中,我想将状态设置为随机数,但如何提取值?正常
r <- randomInt 1 10
在 eval 函数中时不编译。
module Main where
import Prelude
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 e. H.Component { n :: Int } Query e
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 e
eval (NewRandom next) = do
H.modify (\state -> state { n=12 } )
--I'd like to set n to a random number
--but I don't know how.
--let r = randomInt 1 10
--H.modify (\state -> state { n=r } )
pure next
main :: Eff (H.HalogenEffects ()) Unit
main =
runHalogenAff do
body <- awaitBody
H.runUI ui initialState body
你需要在你的 ComponentDSL
中使用合适的 monad(你目前有 e
类型的 var)才能实现,然后你可以使用 H.fromEff
提升randomInt
:
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
r <- H.fromEff $ randomInt 1 10
H.modify (\state -> state { n=r } )
pure next
main :: forall eff. Eff (H.HalogenEffects (random :: RANDOM | eff)) Unit
main =
runHalogenAff do
body <- awaitBody
H.runUI ui initialState body
(旁白:如果你正在做有效的事情,即使你只需要 Eff
,最简单的方法是使用 Aff
作为 ComponentDSL
monad,就像你使用 runUI
它期望它是 Aff
- 可以在 Halogen.Component
模块中使用 interpret
来更改 monad,但是因为你只是使用 interpret liftAff
反正你也可以直接上Aff
.)
查看 "Non-state effects" section of the guide, or the AJAX example 以了解有关 eval
中 运行 效果的更多详细信息。