Posted By

CopperKoo on 05/31/11


Tagged

haskell Concurrent


Versions (?)

takeAllMVars


 / Published in: Haskell
 

Collecting all values in mvar boxes, assuming all mvars are filled at some point. This function does not block on every element of the MVar list, but proceeds with other elements of the list.

  1. import Control.Concurrent
  2.  
  3. rotate :: [a] -> [a]
  4. rotate (x:xs) = xs ++ [x]
  5.  
  6. derotate :: [a] -> [a]
  7. derotate xs = last xs : init xs
  8.  
  9. takeAllMVars :: [MVar a] -> IO [a]
  10. takeAllMVars [] = return []
  11. takeAllMVars (m:ms) = do
  12. x <- tryTakeMVar m
  13. f x
  14. where
  15. f (Just y) = do
  16. ys <- takeAllMVars ms
  17. return $ y:ys
  18. f (Nothing) = do
  19. ys <- takeAllMVars $ rotate $ m:ms
  20. return $ derotate ys

Report this snippet  

You need to login to post a comment.