Skip to content

Instantly share code, notes, and snippets.

@stephanie-gredell
Created December 28, 2020 05:44
Show Gist options
  • Save stephanie-gredell/4f3bb8f0b41e6ad076ca703d764a6904 to your computer and use it in GitHub Desktop.
Save stephanie-gredell/4f3bb8f0b41e6ad076ca703d764a6904 to your computer and use it in GitHub Desktop.
Solution to Elm text field exercise
-- A text input for reversing text. Very useful!
--
-- Read how it works:
-- https://guide.elm-lang.org/architecture/text_fields.html
--
import Browser
import Html exposing (Html, Attribute, div, input, text)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
-- MAIN
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Model =
{ content : String
, length : Int
}
init : Model
init =
{ content = ""
, length = 0 }
-- UPDATE
type Msg
= Change String
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent, length = String.length newContent }
-- VIEW
view : Model -> Html Msg
view model =
div []
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
, div [] [ text (String.reverse model.content) ]
, div [] [ text (String.fromInt model.length) ]
]
@Dave-Lindberg
Copy link

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment