Skip to content

Instantly share code, notes, and snippets.

@napcs
Last active November 19, 2015 16:04
Show Gist options
  • Save napcs/096ae46cfe84e77d396f to your computer and use it in GitHub Desktop.
Save napcs/096ae46cfe84e77d396f to your computer and use it in GitHub Desktop.
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