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
}
Using these stubs for
Event
andpollEvent
and a combinator, borrowed and adapted from an earlier answer, that stops evaluating the first time the predicate fails
allows this ghci session, for example:
You can use monadic lists:
ListT
from the "List" package on hackage.You could use something like:
Instead of:
you can also use:
liftM (x:) (takeWhileM p act)
yielding:Then you can use:
takeWhileM (/=SDL.NoEvent) SDL.pollEvent
i eventually stumbled over this code snippet in an actual SDL game from hackage
thanks for your answers btw!
James Cook was so kind to extend monad-loops with this function:
used with SDL: