Skip to content

Instantly share code, notes, and snippets.

@edazpotato
Created December 22, 2021 13:11
Show Gist options
  • Save edazpotato/bee6a95a198bd0ac99a2b926d6ab99a1 to your computer and use it in GitHub Desktop.
Save edazpotato/bee6a95a198bd0ac99a2b926d6ab99a1 to your computer and use it in GitHub Desktop.
-- My first semi-complicated elm program after reading the first couple pages of the docs
-- I based this off of the text input example here (https://guide.elm-lang.org/architecture/text_fields.html) and used only the api documentation to figure the rest out
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
}
init : Model
init =
{ content = "" }
-- UPDATE
type Msg
= Change String
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent }
-- VIEW
uppercaseEveryOtherChar content =
String.fromList (
List.map
(\(i, char) ->
if (Basics.modBy 2 i) == 1 then
Char.toUpper char
else
Char.toLower char
)
(List.indexedMap Tuple.pair (String.toList content))
)
view : Model -> Html Msg
view model =
div []
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] []
, div [] [ text (String.reverse (uppercaseEveryOtherChar model.content)) ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment