What is the correct way of initializing an elm application

后端 未结 4 1028
有刺的猬
有刺的猬 2020-12-31 02:46

The documentation for Elm\'s Random module states:

A good way to get an unexpected seed is to use the current time. http://package.elm-

4条回答
  •  失恋的感觉
    2020-12-31 03:18

    I reworked the third example from @Apanatshka above, trying to get to simpler code that feels more like the standard architecture, at least as seen in Mike Clark's training videos, and runs under Elm 0.16. Here is the refactored version I came up with:

    module PortBasedRandom where
    
    import Mouse
    import Signal exposing (Signal, map)
    import Random exposing (Seed)
    import Graphics.Element exposing (Element, show)
    
    port primer : Float
    
    
    firstSeed : Seed
    firstSeed =
      Random.initialSeed <| round primer
    
    
    type alias Model =
      { nextSeed : Seed
      , currentInt : Int
      }
    
    
    initialModel : Model
    initialModel =
      { nextSeed = firstSeed
      , currentInt = 0
      }
    
    
    randomInt : Model -> Model
    randomInt model =
      let
          (i, s) = Random.generate (Random.int 1 10) model.nextSeed
      in
          { model | nextSeed = s, currentInt = i }
    
    
    update : (Int, Int) -> Model -> Model
    update (_, _) model =
      randomInt model
    
    
    main : Signal Element
    main =
      Signal.foldp update initialModel Mouse.position
        |> map (\m -> show m.currentInt)
    

    This needs special help in the HTML file, so here's a file containing two examples:

    
      
        
        
      
      
        

    Move your mouse to generate new random numbers between 1 and 10 inclusive.

提交回复
热议问题