
3 Dec
2007
3 Dec
'07
9:52 p.m.
Ryan Ingram said:
Interesting, although this seems like a perfect use for "orelse":
wait_stm :: Wait a -> STM a wait_stm (Wait w) = readTVar w >>= maybe retry return
wait :: Wait a -> IO a wait w = atomically $ wait_stm w
wait_first :: [Wait a] -> IO (a, [Wait a]) wait_first [] = error "wait_first: nothing to wait for" wait_first ws = atomically (do_wait ws) where do_wait [] = retry do_wait (w : ws) = do r <- wait_stm w return (r, ws) `orelse` fmap (second (w:)) (do_wait ws)
Indeed, that is very nice. I see now that orElse allows wait_stm to compose easily, so you don't need to keep opening up the insides of the Wait variable.