From 35db20b196c9cc4a0115159e3f0b47ebf4749ba8 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Wed, 27 Jul 2016 00:21:56 +0200 Subject: [PATCH] first commit --- .gitignore | 2 ++ Counter.elm | 26 +++++++++++++++++++++++++ Main.elm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ elm-package.json | 15 +++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 Counter.elm create mode 100644 Main.elm create mode 100644 elm-package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..766fff3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +elm-stuff/ +repl-temp-* \ No newline at end of file diff --git a/Counter.elm b/Counter.elm new file mode 100644 index 0000000..792cba4 --- /dev/null +++ b/Counter.elm @@ -0,0 +1,26 @@ +import Html exposing (Html, button, div, text) +import Html.App as Html +import Html.Events exposing (onClick) + +main : Program Never +main = Html.beginnerProgram { model = model + , view = view + , update = update + } + +type alias Model = Int +model : Model +model = 0 + +type Msg = Increment | Decrement + +update : Msg -> Model -> Model +update msg model = case msg of + Increment -> model + 1 + Decrement -> model - 1 +view : Model -> Html Msg +view model = div [] + [ button [ onClick Decrement ] [text "-"] + , div [] [text (toString model)] + , button [ onClick Increment ] [text "+"] + ] diff --git a/Main.elm b/Main.elm new file mode 100644 index 0000000..03cee03 --- /dev/null +++ b/Main.elm @@ -0,0 +1,50 @@ +import Html exposing (Html, button, div, text) +import Html.App as App +import Time exposing (Time,second) +import Html.Events exposing (onClick) + +main : Program Never +main = App.program { init = init + , view = view + , update = update + , subscriptions = subscriptions + } + +-- MODEL + +type alias Model = { money : Int + , t : Time + } + +init : (Model,Cmd Msg) +init = ({ money = 0 + , t = 0 + }, Cmd.none) + +-- UPDATE + +type Msg = Gain Int | Tick Time | Reset + +update : Msg -> Model -> (Model, Cmd Msg) +update msg model = + let newmodel = case msg of + Gain i -> { model | money = (model.money + i) } + Tick newTime -> { model | money = model.money + (if model.t > 0 + then truncate ((newTime - model.t) / 1000) + else 0) + , t = newTime } + Reset -> { model | money = 0 } + in (newmodel,Cmd.none) + +-- SUBSCRIPTION + +subscriptions : Model -> Sub Msg +subscriptions model = Time.every second Tick + +view : Model -> Html Msg +view model = div [] + [ button [ onClick (Gain 1) ] [text "+1"] + , button [ onClick (Gain 2) ] [text "+2"] + , div [] [text (toString (.money model))] + , button [ onClick Reset ] [text "0"] + ] diff --git a/elm-package.json b/elm-package.json new file mode 100644 index 0000000..110e106 --- /dev/null +++ b/elm-package.json @@ -0,0 +1,15 @@ +{ + "version": "1.0.0", + "summary": "helpful summary of your project, less than 80 characters", + "repository": "https://github.com/user/project.git", + "license": "BSD3", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "elm-lang/core": "4.0.3 <= v < 5.0.0", + "elm-lang/html": "1.1.0 <= v < 2.0.0" + }, + "elm-version": "0.17.1 <= v < 0.18.0" +}