Created
May 24, 2011 17:38
-
-
Save sordina/989220 to your computer and use it in GitHub Desktop.
Example of closable channels using forkJoin and random sleeps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Closable | |
import ForkJoin | |
import Control.Concurrent | |
import System.Random | |
import Control.Monad | |
main = do | |
c <- newChan :: IO (Chan (Maybe String)) | |
forkIO $ getChanWhileJust c >>= mapM_ print | |
forkJoin [ foo c "test_a.txt" | |
, foo c "test_b.txt"] | |
writeChan c Nothing | |
foo c f = do | |
text <- fmap lines (readFile f) | |
rands <- fmap (randomRs (0::Float, 0.5)) newStdGen | |
zipWithM_ (bar c) rands text | |
bar c x y = do | |
threadDelay . floor . (* 10^6) $ x | |
writeChan c $ Just y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment