Created
June 30, 2015 03:57
-
-
Save cschneid/0fdcbfafd8f9d748c579 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
----------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment