<div dir="ltr">Thanks! That is exactly what I was looking for.<div>Grant</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Jul 6, 2014 at 5:54 AM, Sjoerd Visscher <span dir="ltr"><<a href="mailto:sjoerd@w3future.com" target="_blank">sjoerd@w3future.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You can use the Concurrently newtype wrapper for this, then you can use the Applicative and Alternative type classes.<br>

<br>
dostuff :: Int -> Concurrently (String, Int)<br>
dostuff n = Concurrently $ do<br>
  ..<br>
<br>
let aborcd  =  (\x y -> [x, y]) <$> dostuff 6 <*> dostuff 12<br>
           <|> (\x y -> [x, y]) <$> dostuff 8 <*> dostuff 10<br>
fromMaybe [] <$> timeout (1000000 * 20) (runConcurrently aborcd)<br>
<br>
or even<br>
<br>
let aborcd = traverse dostuff [6, 12] <|> traverse dostuff [8, 10]<br>
<br>
Sjoerd<br>
<div><div class="h5"><br>
On 06 Jul 2014, at 06:17, Michael Snoyman <<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>> wrote:<br>
<br>
><br>
><br>
><br>
> On Sat, Jul 5, 2014 at 3:58 AM, grant weyburne <<a href="mailto:gbwey9@gmail.com">gbwey9@gmail.com</a>> wrote:<br>
> Hi Cafe,<br>
><br>
> Is there any way to simplify this async code to somehow merge the withAsync code<br>
> into the STM code? Looking at the code for tstABorCD it is not easy to see that<br>
> it is really just (A&&B)||(C&&D). Here is the code:<br>
><br>
> <a href="http://lpaste.net/106945" target="_blank">http://lpaste.net/106945</a><br>
><br>
> Thanks for any pointers,<br>
> Grant<br>
><br>
><br>
><br>
> 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):<br>

><br>
> let toList (x, y) = [x, y]<br>
>     ab = toList <$> concurrently (doStuff 6) (doStuff 12)<br>
>     cd = toList <$> concurrently (doStuff 8) (doStuff 10)<br>
> res <- timeout (1000000 * 20) $ race ab cd<br>
> return $ maybe [] (either id id) res<br>
><br>
> Michael<br>
</div></div>> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br>
</blockquote></div><br></div>