Last active
May 17, 2016 03:36
-
-
Save eezis/a52e6275a0b61d24ac8452a835e1a44a to your computer and use it in GitHub Desktop.
messing around with elm -- take input, reverse it, upper case it, count chars, change color if length is even
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
{- | |
for v 0.17 | |
http://guide.elm-lang.org/architecture/user_input/text_fields.html | |
1. reverse and uppercase input. Check | |
2. count & display the number of characters. Check | |
3. change text color if even number of characters . . . Check, but ugly. | |
-} | |
import Html exposing (Html, Attribute, text, div, input) | |
import Html.App exposing (beginnerProgram) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onInput) | |
import String exposing (..) | |
main = | |
beginnerProgram { model = "", view = view, update = update } | |
-- UPDATE | |
type Msg = NewContent String | |
update (NewContent content) oldContent = | |
content | |
-- VIEW | |
reverseAndUpper: String -> String | |
reverseAndUpper s = | |
reverse s | |
|> toUpper | |
{- | |
even_or_odd : Int -> String | |
even_or_odd x = | |
if x % 2 == 0 then | |
"green" | |
else | |
"red" | |
-} | |
blueIfEven : String -> Attribute msg | |
blueIfEven s = | |
let | |
blue = | |
style | |
[ ("width", "100%") | |
, ("height", "40px") | |
, ("padding", "10px 0") | |
, ("font-size", "2em") | |
, ("text-align", "center") | |
, ("color", "blue") | |
] | |
in | |
if (String.length s) % 2 == 0 then | |
blue | |
else | |
style | |
[ ("width", "100%") | |
, ("height", "40px") | |
, ("padding", "10px 0") | |
, ("font-size", "2em") | |
, ("text-align", "center") | |
] | |
{- | |
getStyle = | |
let | |
theStyle = | |
[ blueIfEven content ] | |
in | |
theStyle | |
-} | |
view content = | |
div [] | |
[ input [ placeholder "Text to reverse", onInput NewContent, myStyle ] [] | |
, div [ blueIfEven content ] [ text (reverseAndUpper content) ] | |
, div [ myStyle ] [ text (toString (String.length content)) ] | |
] | |
myStyle = | |
style | |
[ ("width", "100%") | |
, ("height", "40px") | |
, ("padding", "10px 0") | |
, ("font-size", "2em") | |
, ("text-align", "center") | |
] |
GetContented
commented
May 17, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment