[reactive] Laziness and IO adapters
sasha.rush at gmail.com
Mon Nov 10 21:53:19 EST 2008
Tried putting this on "Introducing Reactive: Events." But was having
commenting issues. I wanted to make the BellMachines actually work
with on the command line so I tried the following code.
> runMachine :: BellMachine -> IO ()
> runMachine machine = do
> -- get button press
> clock <- makeClock
> (buttonPressed, buttonPressedSink) <- makeEvent clock
> forkIO $ buttonPresses buttonPressedSink
> adaptE (fmap bell $ machine buttonPressed)
> -- run the machine
> where bell = const $ print "beep!"
> buttonPresses sink = sequence_ $ repeat $ (getChar >> sink ())
> main = runMachine metronome
This guy works fine for the basic machines but it breaks for
metronome. The first reason seems to be that when it gets to a point
when there are no future events, switchE fails.
"Exception: Future mempty: it'll never happen, buddy"
I could hack around this, but then I got to another issue. It seems
like switchE is too lazy, which causes my events always fire one click
behind when I press the button. My guess is that since switchE is
implemented with withRestE, it needs to peak ahead one event.
Not sure how to deal with these issues in practice. Curious if I'm
just doing something wrong.
More information about the Reactive