On 11/27/07, Matthew Brecknell <haskell@brecknell.org> wrote:
> 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 (Wait w : ws) = do
> r <- readTVar w
> case r of
> Nothing -> fmap (second (Wait w:)) (do_wait ws)
> Just s -> return (s,ws)
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)