I'm playing around with the netwire package trying to get a feel for FRP, and I have a quick question.
Starting with the following simple wires, I'm able to emit an event every 5 seconds (approx)
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 map each event produced to a wire, that I can then watch update. I have an accumulator function like the following:
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
I then introduce a new wire that will inhibit until the eventList
starts triggering events, like so:
myList :: (Monad m, HasTime t s) => Wire s () m a [Wire s () m a Int]
myList = asSoonAs . eventList
So I've gone from events to a wire containing a list of wires. Finally, I introduce a wire to step each of these wires and produce 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 routine to test it all out:
main = testWire clockSession_ myNumList
What I expect to see is a growing list, where each element in the list will show it's creation time for 10 seconds, after which the element will show a zero. What I'm getting instead is a growing list of static values. For example, what I expect to see after a few steps is
[0]
[5, 0]
[10, 5, 0]
[15, 10, 0, 0]
and so on. What I'm actually seeing is
[0]
[5, 0]
[10, 5, 0]
[15, 10, 5, 0]
So I know my accumulator function is working: every event created is being converted into a wire. But what I'm not seeing are these wires emitting different values over time. My statement for 10 . pure x --> pure 0
should be switching them over to emitting 0 after the time has elapsed.
I am still new to FRP, so I may be fundamentally misunderstanding something important about it (probably the case.)