0

I wanted to have in Haskell a very simple code in which events are handled.

For instance, I'm looking for something like this:

main = do
    firstEvent <- waitForEvent
    -- Do Something with that... --

waitForEvent = do
    letter <- getChar
    OR
    sth <- getSomethingFromSocket
    OR
    -- etc --

I wanted to get the first event that take place without the 'busy waiting'.

Thanks!

dani24
  • 2,108
  • 2
  • 26
  • 28
  • 1
    If you decide to use [`async`](https://hackage.haskell.org/package/async) then [`waitAny`](https://hackage.haskell.org/package/async-2.1.0/docs/Control-Concurrent-Async.html#waitAny) might be what you want. – Alec Apr 20 '16 at 00:01
  • @Wagner I want an example, and not just a clarification (as it is answered in the "duplicated" question). I think the Alec's comment is worth for me, But should I use like a `(a, b, ...) <- concurrently getFirstEvent getSecondEvent ...` and then check if one of those is instanced or what ? – dani24 Apr 20 '16 at 03:31
  • 1
    If you just want to wait for one of either of those occurrences (so you are waiting for exactly one of `getChar` or `getSomethingFromSocket`) you can use [`race`](https://hackage.haskell.org/package/async-2.1.0/docs/Control-Concurrent-Async.html#v:race). Definitely not `concurrently`, since that will block until BOTH (not either) events occur. – Alec Apr 20 '16 at 03:56

0 Answers0