Skip to content

Instantly share code, notes, and snippets.

@chrisbuttery
Last active June 22, 2018 08:25
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 chrisbuttery/9830823fc3e90d7bd2a6 to your computer and use it in GitHub Desktop.
Save chrisbuttery/9830823fc3e90d7bd2a6 to your computer and use it in GitHub Desktop.
A countdown timer in Elm : elm-lang http://elm-lang.org
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
@adrianmcli
Copy link

Hi there, since Signals are no longer in the current version of Elm, I was wondering how you would refactor this @chrisbuttery?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment