-
-
Save Mokosha/6a78c84a26971251c771 to your computer and use it in GitHub Desktop.
Program using a wire to produce persistent wires.
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
import Control.Wire | |
import Prelude hiding ((.)) | |
myWire :: (Monad m, HasTime t s) => Wire s () m a Float | |
myWire = timeF | |
myWire' :: (Monad m, HasTime t s) => Wire s () m a Int | |
myWire' = fmap round myWire | |
myEvent :: (Monad m, HasTime t s) => Wire s () m a (Event Int) | |
myEvent = periodic 5 . myWire' | |
eventWire :: (Monad m, HasTime t s) | |
=> Wire s () m a (Event (Wire s () m a Int)) | |
eventWire = (fmap f) <$> myEvent | |
where f x = for 10 . pure x --> pure 0 | |
-- This wire inhibits unless the event happens, in which case it | |
-- passes on the event value | |
pulseWire :: (Monad m, HasTime t s) => Wire s () m (Event a) a | |
pulseWire = | |
(rSwitch mkEmpty) . | |
(mkId &&& (mkSF_ $ fmap (\x -> mkSFN (\_ -> (x, pulseWire))))) | |
myListElement :: (Monad m, HasTime t s) => Wire s () m a (Wire s () m a Int) | |
myListElement = pulseWire . eventWire | |
myNums :: (Monad m, HasTime t s) => | |
[Wire s () m a Int] -> | |
Wire s () m a (Wire s () m a Int) -> | |
Wire s () m a [Int] | |
myNums wires listElement = mkGen $ \dt val -> do | |
(newElement, newElementWire) <- stepWire listElement dt (Right val) | |
ws <- case newElement of | |
Left _ -> return wires | |
Right w -> return (w : wires) | |
stepped <- mapM (\w -> stepWire w dt (Right val)) ws | |
let alive = [ (r, w) | (Right r, w) <- stepped ] | |
return (Right (map fst alive), myNums (map snd alive) newElementWire) | |
myNumList :: (Monad m, HasTime t s) => Wire s () m a [Int] | |
myNumList = myNums [] myListElement | |
main = testWire clockSession_ myNumList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment