Skip to content

Instantly share code, notes, and snippets.

@avh4
Last active July 15, 2016 17:56
Show Gist options
  • Save avh4/0e659685a2103ad2995012f5e9e1e276 to your computer and use it in GitHub Desktop.
Save avh4/0e659685a2103ad2995012f5e9e1e276 to your computer and use it in GitHub Desktop.
Intro to Elm - SF Elm meetup 2016-07-14
module Main exposing (..)
import Html.App
import WordThing
-- WIRING
main : Program Never
main =
Html.App.beginnerProgram
{ model = WordThing.initialModel
, update = WordThing.update
, view = WordThing.view
}
module WordThing exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import String
-- MODEL
type alias Model =
{ text : String
, changesCount : Int
}
initialModel : Model
initialModel =
{ text = ""
, changesCount = 0
}
-- UPDATE
type Msg
= TextChanged String
| Clear
update : Msg -> Model -> Model
update msg model =
case msg of
TextChanged newText ->
{ model
| text = newText
, changesCount =
model.changesCount + 1
}
Clear ->
{ model
| text = ""
, changesCount = 0
}
-- VIEW
splitParagraphs : String -> List (Html Msg)
splitParagraphs inputString =
let
paragraphStrings =
String.split "\n\n" inputString
wrapInP string =
p [] [ text string ]
in
List.map wrapInP paragraphStrings
view : Model -> Html Msg
view model =
p []
[ text "Current: "
, model.text
|> splitParagraphs
|> div []
, div
[ style [ ( "background-color", "red" ) ]
]
(splitParagraphs model.text)
, hr [] []
, text
("Times changed: "
++ toString model.changesCount
)
, hr [] []
, text
("Characters: "
++ toString (String.length model.text)
)
, hr [] []
, button
[ onClick Clear ]
[ text "Clear" ]
, textarea
[ rows 20
, cols 50
, onInput TextChanged
, value model.text
]
[]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment