Skip to content

Instantly share code, notes, and snippets.

@cschneid
Created June 30, 2015 03:57
Show Gist options
  • Select an option

  • Save cschneid/0fdcbfafd8f9d748c579 to your computer and use it in GitHub Desktop.

Select an option

Save cschneid/0fdcbfafd8f9d748c579 to your computer and use it in GitHub Desktop.

Revisions

  1. cschneid created this gist Jun 30, 2015.
    70 changes: 70 additions & 0 deletions Counter.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    import Signal exposing (Signal, Address)
    import Html exposing (Html, div, button, text)
    import Html.Events exposing (onClick)
    import Task

    main : Signal Html
    main =
    start { model = model
    , view = view
    , update = update }

    type alias Model = Int

    model : Model
    model = 0

    view : Address Action -> Model -> Html
    view address model =
    div []
    [ button [ onClick address Decrement ] [ text "-" ]
    , div [] [ text (toString model) ]
    , button [ onClick address Increment ] [ text "+" ]
    ]

    type Action = Increment
    | Decrement

    update : Action -> Model -> Model
    update action model =
    case action of
    Increment -> model + 1
    Decrement -> model - 1


    -----------------------------------------------------------
    -- StartApp
    -----------------------------------------------------------
    type alias App model action =
    { model : model
    , view : Address action -> model -> Html
    , update : action -> model -> model
    }

    start : App Model Action -> Signal Html
    start app =
    let
    actions : Signal.Mailbox (Maybe Action)
    actions =
    Signal.mailbox Nothing

    address : Signal.Address Action
    address =
    Signal.forwardTo actions.address Just

    step : Maybe Action -> Model -> Model
    step (Just action) m =
    app.update action m

    model : Signal Model
    model =
    Signal.foldp
    step
    app.model
    actions.signal

    in
    Signal.map (app.view address) model
    -----------------------------------------------------------
    -- End StartApp
    -----------------------------------------------------------