代码之家  ›  专栏  ›  技术社区  ›  crazhyouse

Purescript卤素,副作用(随机数)

  •  1
  • crazhyouse  · 技术社区  · 8 年前

    在PureScript卤素项目中,我想将状态设置为随机数,但如何提取值?正常的

    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
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   gb.    8 年前

    您需要在您的 ComponentDSL (如果您有 e 输入var currently)使其成为可能,然后可以使用 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 作为 组件DSL monad,当您使用 runUI 它希望它是 Aff公司 -可以使用以下命令更改monad interpret Halogen.Component 模块,但由于您只使用 interpret liftAff 无论如何,你最好直接去 Aff公司 .)

    看看 "Non-state effects" section of the guide ,或 AJAX example 有关跑步效果的详细信息,请参见 eval .