-
-
Save BenSchZA/57f1f46ec8e2ecc0f304ede813e5f4eb to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE Arrows #-} | |
{-# LANGUAGE BlockArguments #-} | |
module Main where | |
import Control.Concurrent | |
import FRP.Yampa | |
data State = State { value :: Double } deriving (Show) | |
initialState = State 25.0 | |
cooling :: State -> SF () (State) | |
cooling state = (constant (-1) >>> integral) >>^ (+ value state) >>> arr (State) | |
coolingWithFloor :: State -> SF () (State) | |
coolingWithFloor state = switch cooling' atRoomTemp | |
where t' = cooling state | |
cooling' = ((arr (\_ -> t')) &&& ((\_ -> t') ^>> value ^>> (<= 18) ^>> edge >>^ (\e -> e `tag` t'))) | |
atRoomTemp _ = (constant 18) | |
main :: IO () | |
main = reactimate (return ()) | |
(\_ -> threadDelay 1 >> return (0.1, Nothing)) | |
(\_ b -> (putStrLn $ show b) >> return False) | |
(coolingWithFloor initialState) |
Author
BenSchZA
commented
Nov 12, 2020
Original code:
cooling :: Double -> SF () (Double)
cooling t0 = proc input -> do
t0' <- integral >>^ (+ t0) -< -1
returnA -< t0'
coolingWithFloor :: Double -> SF () (Double)
coolingWithFloor t0 = switch cooling' atRoomTemp
where cooling' = proc _ -> do
t' <- cooling t0 -< ()
e <- edge -< t' <= 18
returnA -< (t', e `tag` t')
atRoomTemp _ = (constant 18)
main :: IO ()
main = reactimate (return ())
(\_ -> threadDelay 100000 >> return (0.1, Nothing))
(\_ b -> (putStrLn $ show b) >> return False)
(coolingWithFloor 25.0)
The solution!
{-# LANGUAGE Arrows #-}
{-# LANGUAGE BlockArguments #-}
module Main where
import Control.Concurrent
import FRP.Yampa
data State = State { value :: Double } deriving (Show)
initialState = State 25.0
cooling :: State -> SF () (State)
cooling state = (constant (-1) >>> integral) >>^ (+ value state) >>^ State
coolingWithFloor :: State -> SF () (State)
coolingWithFloor state = switch cooling' atRoomTemp
where t' :: SF () State
t' = cooling state
branch_a :: SF () State
branch_a = t'
branch_b :: SF () (Event (SF () State))
branch_b = (t' >>> value ^>> (<= 18) ^>> edge >>^ (\e -> e `tag` t'))
cooling' :: SF () (State, Event (SF () State))
cooling' = branch_a &&& branch_b
atRoomTemp :: p -> SF b State
atRoomTemp = (\_ -> constant 18 >>^ State)
main :: IO ()
main = reactimate (return ())
(\_ -> threadDelay 100000 >> return (0.1, Nothing))
(\_ b -> (putStrLn $ show b) >> return False)
(coolingWithFloor initialState)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment