Skip to content

Instantly share code, notes, and snippets.

@thotmx
Last active September 9, 2017 15:12
Show Gist options
  • Save thotmx/f0b0a1b5b97ccb4d8f301d482a366fb8 to your computer and use it in GitHub Desktop.
Save thotmx/f0b0a1b5b97ccb4d8f301d482a366fb8 to your computer and use it in GitHub Desktop.
Simple Elm example for an Oaxaca.rb talk
import Html exposing (..)
import Html.Attributes exposing(placeholder)
import Html.Events exposing(onInput, onClick)
import Random exposing (..)
import Random.String
import Random.Char
type alias Model = {
content : String
}
-- MODEL
initialModel : Model
initialModel = { content = "Hola" }
-- UPDATE
type Msg = Update String | Reset
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Reset ->
( { model | content = "" } , generateRandomString)
Update newString ->
( { model | content = newString }, Cmd.none)
-- COMMANDS
generateRandomString : Cmd Msg
generateRandomString =
Random.generate Update (Random.String.string 5 Random.Char.english)
-- VIEW
view model = div [] [
input [ placeholder "Escribe algo", onInput Update ] []
, h1 [] [ text model.content ]
, button [onClick Reset] [ text "Reset" ]
]
-- MAIN
main = program {
init = ( initialModel, Cmd.none)
, update = update
, view = view
, subscriptions = ( \_ -> Sub.none )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment