How can I issue multiple calls to SDL.pollEvent :: IO Event until the output is SDL.NoEvent and collect all the results into a list?
In imperative terms something like this:
events = []
event = SDL.pollEvent
while ( event != SDL.NoEvent ) {
events.add( event )
event = SDL.pollEvent
}
James Cook was so kind to extend monad-loops with this function:
unfoldWhileM :: Monad m => (a -> Bool) -> m a -> m [a]
used with SDL:
events <- unfoldWhileM (/= SDL.NoEvent) SDL.pollEvent
You could use something like:
takeWhileM :: (a -> Bool) -> IO a -> IO [a]
takeWhileM p act = do
x <- act
if p x
then do
xs <- takeWhileM p act
return (x : xs)
else
return []
Instead of:
do xs <- takeWhileM p act return (x : xs)
you can also use:
liftM (x:) (takeWhileM p act) yielding:
takeWhileM :: (a -> Bool) -> IO a -> IO [a]
takeWhileM p act = do
x <- act
if p x
then liftM (x:) (takeWhileM p act)
else return []
Then you can use: takeWhileM (/=SDL.NoEvent) SDL.pollEvent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With