Skip to content

Instantly share code, notes, and snippets.

@Mokosha
Created March 8, 2015 02:15
Show Gist options
  • Save Mokosha/6a78c84a26971251c771 to your computer and use it in GitHub Desktop.
Save Mokosha/6a78c84a26971251c771 to your computer and use it in GitHub Desktop.
Program using a wire to produce persistent wires.
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