elm-make
elm-package install elm-lang/html
elm-make Main.elm && open index.html
elm-reactor
Last active
June 23, 2016 15:10
-
-
Save avh4/49ac26a2bcb6c8c6e0b0dd3d6ec7811d to your computer and use it in GitHub Desktop.
Intro to Elm - SF Elm meetup 2016-06-22
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
module Main exposing (..) | |
import Html.App | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onClick, onInput) | |
import String | |
type alias Model = | |
{ count : Int | |
, textInput : Int | |
} | |
type Msg | |
= Increment | |
| Decrement | |
| MultiplyBy Int | |
| TextChanged String | |
| IncerementByTextFieldAmount | |
main = | |
Html.App.beginnerProgram | |
{ model = initialModel | |
, update = update | |
, view = view | |
} | |
initialModel : Model | |
initialModel = | |
{ count = 0 | |
, textInput = 0 | |
} | |
update : Msg -> Model -> Model | |
update action model = | |
case action of | |
Increment -> | |
{ model | count = model.count + 1 } | |
Decrement -> | |
{ model | count = model.count - 1 } | |
MultiplyBy howMuch -> | |
{ model | count = model.count * howMuch } | |
TextChanged text -> | |
case (String.toInt text) of | |
Ok parsedInt -> | |
{ model | textInput = parsedInt } | |
Err errorMessage -> | |
model | |
IncerementByTextFieldAmount -> | |
{ model | count = model.count + model.textInput } | |
handleTextChange : String -> Msg | |
handleTextChange text = | |
TextChanged text | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ text ("Current count: " ++ toString model.count) | |
, button [ onClick Increment ] [ text "Increment" ] | |
, button [ onClick Decrement ] [ text "Decrement" ] | |
, button [ onClick (MultiplyBy 3) ] [ text "x3" ] | |
, button [ onClick (MultiplyBy 10) ] [ text "x10" ] | |
, hr [] [] | |
, input [ onInput handleTextChange ] [] | |
, button [ onClick IncerementByTextFieldAmount ] | |
[ text "Increment by that much" ] | |
, hr [] [] | |
, pre [] [ text (toString model) ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment