Skip to content

Instantly share code, notes, and snippets.

@janlonden
Last active June 23, 2019 17:20
Show Gist options
  • Save janlonden/9ef44a4e7a301809f4fa00fa3abeaba4 to your computer and use it in GitHub Desktop.
Save janlonden/9ef44a4e7a301809f4fa00fa3abeaba4 to your computer and use it in GitHub Desktop.
import Browser
import Html exposing (Html, div, text)
import Html.Events exposing (onClick)
main =
Browser.sandbox
{ init = init
, update = update
, view = view
}
type alias Todo =
{ id: Int
, isDone: Bool
, task: String
}
type alias Model = List Todo
init : Model
init =
[ { id = 1, isDone = False, task = "My first todo" }
, { id = 2, isDone = False, task = "My second todo" }
, { id = 3, isDone = False, task = "My third todo" }
]
type Msg = MarkDone Int
update : Msg -> Model -> Model
update msg model =
case msg of
MarkDone id ->
List.map
(ifElse (\a -> a.id == id) (\a -> { a | isDone = not a.isDone }) identity)
model
view : Model -> Html Msg
view model =
div [] (List.map createTodo model)
createTodo : Todo -> Html Msg
createTodo todo =
div
[ onClick (MarkDone todo.id) ]
[ text ((ifElse .isDone (always "[x] ") (always " [ ] ") todo) ++ todo.task) ]
ifElse : (a -> Bool) -> (a -> b) -> (a -> b) -> a -> b
ifElse conditionFn trueFn falseFn value =
if (conditionFn value) then
trueFn value
else
falseFn value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment