I play with the netwire package, trying to understand FRP, and I have a quick question.
Starting with the following simple wires, I can emit an event every 5 seconds (approximately)
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'
This is pretty nice and straight forward, but what I want to do next is to compare each event with the wiring, after which I can see the update. I have a battery function such as:
eventList :: (Monad m, HasTime t s)
=> Wire s () m a (Event [Wire s () m a Int])
eventList = accumE go [] . myEvent
where go soFar x = f x : soFar
f x = for 10 . pure x --> pure 0
Then I introduce a new wire that will block until eventListit starts to fire events, for example:
myList :: (Monad m, HasTime t s) => Wire s () m a [Wire s () m a Int]
myList = asSoonAs . eventList
So, I switched from events to a wire containing a list of wires. Finally, I introduced a wire for each of these wires and compiled a list of results:
myNums :: (Monad m, HasTime t s) => Wire s () m [Wire s () m a Int] [Int]
myNums = mkGen $ \dt wires -> do
stepped <- mapM (\w -> stepWire w dt $ Right undefined) wires
let alive = [ (r, w) | (Right r, w) <- stepped ]
return (Right (map fst alive), myNums)
myNumList :: (Monad m, HasTime t s) => Wire s () m a [Int]
myNumList = myNums . myList
And finally, I have my main procedure to check everything:
main = testWire clockSession_ myNumList
, , - , 10 , . . , , ,
[0]
[5, 0]
[10, 5, 0]
[15, 10, 0, 0]
..
[0]
[5, 0]
[10, 5, 0]
[15, 10, 5, 0]
, , : . , . for 10 . pure x --> pure 0 0 .
FRP, - (, .)