Skip to content

Instantly share code, notes, and snippets.

@sli
Created August 23, 2018 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sli/9001c24f4a8fad3338a42bf6b59daeae to your computer and use it in GitHub Desktop.
Save sli/9001c24f4a8fad3338a42bf6b59daeae to your computer and use it in GitHub Desktop.
Some random, useless Elm shit that I just want to show to people.
import Html exposing (Html, Attribute, text, program, section, div, button, input, label)
import Html.Events exposing (onClick)
import Html.Attributes exposing (class, type_, placeholder)
type alias Todo =
{ id : Int
, description : String
, done : Bool
}
type alias Model = { todos : List Todo }
type Msg
= Reset
| AddTodo { id : Int, description : String }
| RemoveTodo { id : Int }
| CheckTodo { id : Int }
init : ( Model, Cmd msg )
init = ( { todos = [] }, Cmd.none )
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
let newModel =
case msg of
Reset -> { todos = [] }
AddTodo t -> { model | todos = { id = t.id, description = t.description, done = False } :: model.todos }
RemoveTodo t -> { model | todos = List.filter (\e -> e.id == t.id) model.todos }
CheckTodo t -> { model | todos = List.map (\e -> if e.id == t.id then { e | done = not e.done } else e) model.todos }
in
( newModel, Cmd.none )
view : Model -> Html Msg
view model =
section [ class "section" ]
[ div [ class "field has-addons" ]
[ div [ class "control is-expanded" ] [ input [ class "input", type_ "text", placeholder "Description" ] [] ]
, div [ class "control" ] [ button [ class "button is-primary" ] [ text "Add Todo" ] ]
]
]
subscriptions : Model -> Sub Msg
subscriptions model = Sub.none
main = program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment