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

Matthew Brecknell haskell at brecknell.org
Mon Dec 3 21:52:42 EST 2007


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.



More information about the Haskell-Cafe mailing list