Last active
June 22, 2018 08:25
-
-
Save chrisbuttery/9830823fc3e90d7bd2a6 to your computer and use it in GitHub Desktop.
A countdown timer in Elm : elm-lang http://elm-lang.org
This file contains 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
module CountDown where | |
import Graphics.Element exposing (..) | |
import Time exposing (..) | |
import String | |
-- MODEL | |
minutes = 1 | |
seconds = 30 | |
type alias Model = | |
{ m: Int | |
, s: Int | |
, minutes: String | |
, seconds: String | |
} | |
initialModel: Model | |
initialModel = | |
{ m = minutes | |
, s = seconds | |
, minutes = toString minutes | |
, seconds = toString seconds | |
} | |
-- UPDATE | |
zeroPadNumber n = | |
toString n |> String.padLeft 2 '0' | |
update: Time.Time -> Model -> Model | |
update timestamp model = | |
let | |
minutes = | |
if model.m > 0 && model.s > 0 | |
then model.m | |
else if model.m > 0 && model.s == 0 | |
then model.m - 1 | |
else 0 | |
seconds = | |
if model.s > 0 | |
then model.s - 1 | |
else if model.m > 0 && model.s == 0 | |
then 59 | |
else 0 | |
in | |
{ model | | |
m = minutes | |
, s = seconds | |
, minutes = toString minutes | |
, seconds = zeroPadNumber seconds | |
} | |
-- VIEW | |
view: Model -> Element | |
view model = | |
show (model.minutes ++ ":" ++ model.seconds) | |
model: Signal Model | |
model = | |
Signal.foldp update initialModel (every second) | |
main: Signal Element | |
main = | |
Signal.map view model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there, since Signals are no longer in the current version of Elm, I was wondering how you would refactor this @chrisbuttery?