Skip to content

Instantly share code, notes, and snippets.

@jonaslu
Last active May 29, 2016 10:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonaslu/bac24bce4f2a122b32fbfde975b32e9e to your computer and use it in GitHub Desktop.
Save jonaslu/bac24bce4f2a122b32fbfde975b32e9e to your computer and use it in GitHub Desktop.
First try of a todo-app in elm. It works but it ain't pretty. Created with my parter in crime @popstr
import Html exposing (..)
import Html.App exposing (beginnerProgram)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import List exposing (..)
import String
main =
beginnerProgram { model = model, view = view, update = update }
type alias Model = { items: List String, currentItem: String }
model: Model
model = {
items = [],
currentItem = ""
}
type Msg = Add | Update String | Remove Int
update: Msg -> Model -> Model
update msg model =
case msg of
Add ->
{ items = append model.items [model.currentItem],
currentItem = "" }
Update text ->
{ model | currentItem = text }
Remove idx ->
{ model | items =
(take idx model.items ++ drop (idx+1) model.items)
}
view model =
div []
[ div []
[ ul []
(indexedMap (\ idx item -> (li [] [
text (item ++ " "),
button [onClick (Remove idx)] [ text "X"]
])) model.items)
]
, input [value model.currentItem, onInput Update] []
, button [onClick Add] [ text "+" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment