Created
May 6, 2016 19:58
-
-
Save coproduto/9e900e42540d11a56e18b618928d6f87 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
| import Html as H exposing (Html) | |
| import Html.Attributes as A | |
| import Html.Events as Events | |
| import Signal exposing (Signal, Address) | |
| import StartApp.Simple as App | |
| import List | |
| options : List String | |
| options = | |
| [ "Forte" | |
| , "Suave" | |
| , "Lento" | |
| , "Rápido" | |
| ] | |
| type alias Model = | |
| { votes : List (String, Int) } | |
| type Action = | |
| Vote String | |
| findAndApply : (a -> a) -> b -> List (b, a) -> List (b, a) | |
| findAndApply f x l = | |
| case l of | |
| ((a,b) :: t) -> | |
| if a == x then (a,(f b)) :: t | |
| else (a,b) :: (findAndApply f x t) | |
| [] -> [] | |
| voteList : Address Action -> List (String, Int) -> Html | |
| voteList address l = | |
| let list = | |
| List.map (\(str, n) -> | |
| H.li [] | |
| [ H.text (str ++ " : " ++ (toString n)) | |
| , H.button | |
| [ Events.onClick address (Vote str) ] | |
| [ H.text "Votar" ] | |
| ] | |
| ) l | |
| in H.ul [] list | |
| initialModel : Model | |
| initialModel = | |
| { votes = List.map (\x -> (x,0)) options } | |
| update : Action -> Model -> Model | |
| update (Vote str) model = | |
| { model | votes = findAndApply (\x -> x+1) str model.votes } | |
| view : Address Action -> Model -> Html | |
| view address model = | |
| H.div [] | |
| [ H.div [] | |
| [ voteList address model.votes ] | |
| ] | |
| main : Signal Html | |
| main = | |
| App.start | |
| { model = initialModel | |
| , view = view | |
| , update = update | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment