Created
July 23, 2016 15:51
-
-
Save ifyouseewendy/fc0d8e35e261538bc29997d41d6a5efc to your computer and use it in GitHub Desktop.
Demo code from Richard Feldman's https://www.youtube.com/results?search_query=introduction+to+elm
This file contains hidden or 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 Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
import Html.App as Html | |
-- model | |
type alias Model = | |
{ | |
shelves : Int, | |
username : String | |
} | |
model : Model | |
model = | |
{ shelves = 2, username = "wendi" } | |
-- update | |
type Action = | |
Increment Int | Decrement | |
update : Action -> Model -> Model | |
update action model = | |
case action of | |
Increment quantity -> | |
{ model | shelves = model.shelves + quantity } | |
Decrement -> | |
{ model | shelves = Basics.max 0 (model.shelves - 1) } | |
-- _ -> | |
-- model | |
-- view | |
pluralize : String -> String -> Int -> String | |
pluralize singular plural quantity = | |
if quantity == 1 then | |
singular | |
else | |
plural | |
shelfButton caption action = | |
button [ onClick action ] [ text caption ] | |
view model = | |
let | |
isDisabled = | |
model.shelves <= 0 | |
caption = | |
(toString model.shelves) | |
++ " " | |
++ (pluralize "shelf" "shelves" model.shelves) | |
in | |
div | |
[ class "content", id "main-body" ] | |
[ | |
h1 [] [ text "Pluralizer"], | |
div [] [ shelfButton "Add a shelf" (Increment 10) ], | |
div [] [ shelfButton "Explode a shelf" Decrement ], | |
text caption | |
] | |
main = | |
Html.beginnerProgram | |
{ model = model, update = update, view = view } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment