Last active
November 19, 2015 16:04
-
-
Save napcs/096ae46cfe84e77d396f to your computer and use it in GitHub Desktop.
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
module CharacterCounter where | |
import Html exposing(..) | |
import Html.Attributes exposing(..) | |
import Html.Events exposing(..) | |
import String exposing(..) | |
import Signal exposing (Address) | |
import StartApp.Simple as StartApp | |
type Action | |
= NoOp | |
| Count String | |
model = | |
{ characterCount = 0, | |
wordCount = 0 | |
} | |
view address model = | |
div [] | |
[ | |
textarea | |
[ on "input" targetValue (\value -> Signal.message address (Count value))] | |
[ text "" ], | |
div [] [ | |
span [] [text ("characters: " ++ toString(model.characterCount)) ], | |
span [] [text ("words: " ++ toString(model.wordCount)) ] | |
] | |
] | |
update action model = | |
case action of | |
NoOp -> | |
model | |
Count content -> | |
{model | | |
characterCount = (countCharacters content), | |
wordCount = (countWords content) | |
} | |
countWords text = | |
text | |
|> split " " | |
|> List.length | |
countCharacters text = | |
text | |
|> String.length | |
main = | |
StartApp.start { model = model, view = view, update = update } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment