Skip to content

Instantly share code, notes, and snippets.

@johnpmayer
Last active December 16, 2015 14:20
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 johnpmayer/5448143 to your computer and use it in GitHub Desktop.
Save johnpmayer/5448143 to your computer and use it in GitHub Desktop.
Use case of loop (I did not try to compile this code)
{-
- This is the loop function that I propose as an ehancement.
- I think it's better because the asychonous loopback signal
- is anonymous, rather than a top-level definition.
-}
loop : (b -> a) -> b -> (Signal a -> Signal b) -> Signal b
-- Here's how we're going to control our little demo
powerButton : Element
power : Signal Bool
(powerButton, power) = checkbox True
-- Here's our world state. Comes with an initial value, a draw function, and a step function
type Clock = { hour : Int, min : Int, sec : Int }
midnight : clockState
drawClock : clockState -> Element
tickClock : clockState -> clockState
-- This function updates the new time from the previous time on every tick
tickWhen : Signal Time -> Signal Clock -> Signal Clock
tickWhen tick currentClock = sampleOn tick (tickClock <~ currentClock)
-- Here's where we check the current time
isNotThreePM : Clock -> Bool
shouldClockGo : Signal Clock -> Signal Bool
shouldClockGo currentClock = (&&) <~ power ~ (isNotThreePM <~ currentClock)
-- This function now captures all of the logic of when we should update the new clock signal from some old clock signal
updateClock : Signal Clock -> Signal Clock
updateClock currentClock = tickWhen (fpsWhen 1 (shouldClockGo currentClock) currentClock
-- Here's where loop comes in. The current time is anonymously looped back to be the previous time
currentClock : Signal Clock
currentClock = loop id midnight updateClock
-- I'm being lazy here and conflating [Signal] with Signal[], but i think the meaning is clear
main = (flow down) <~ [constant powerbutton, drawClock currentClock]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment