[Haskell-cafe] async simplify code

Michael Snoyman michael at snoyman.com
Sun Jul 6 04:17:58 UTC 2014


On Sat, Jul 5, 2014 at 3:58 AM, grant weyburne <gbwey9 at gmail.com> wrote:

> Hi Cafe,
>
> Is there any way to simplify this async code to somehow merge the
> withAsync code
> into the STM code? Looking at the code for tstABorCD it is not easy to
> see that
> it is really just (A&&B)||(C&&D). Here is the code:
>
> http://lpaste.net/106945
>
> Thanks for any pointers,
> Grant
>
>
>
The code as-is doesn't actually look too bad to me. But it *might* be a bit
easier to read if instead of withAsync, waitSTM, and registerDelay, you
used race, concurrently, and timeout. That might look something like this
(untested):

let toList (x, y) = [x, y]
    ab = toList <$> concurrently (doStuff 6) (doStuff 12)
    cd = toList <$> concurrently (doStuff 8) (doStuff 10)
res <- timeout (1000000 * 20) $ race ab cd
return $ maybe [] (either id id) res

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140706/b462aa1a/attachment.html>


More information about the Haskell-Cafe mailing list