[Haskell-cafe] Re: Waiting for thread to finish

Ryan Ingram ryani.spam at gmail.com
Mon Dec 3 17:39:56 EST 2007


On 11/27/07, Matthew Brecknell <haskell at 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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071203/2a8ed2b5/attachment.htm


More information about the Haskell-Cafe mailing list