Created
February 9, 2019 20:11
-
-
Save matfournier/02febbf760a045742199b75cd30cad9f to your computer and use it in GitHub Desktop.
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
module Chapter8.GetURLTwo where | |
import Chapter8.MFGetURL | |
import Control.Concurrent | |
import qualified Data.ByteString as B | |
data Async a = Async (MVar a) | |
async' :: IO a -> IO (Async a) | |
async' action = do | |
var <- newEmptyMVar | |
forkIO (do r <- action; putMVar var r) | |
return (Async var) | |
async'' :: IO a -> IO (Async a) | |
async'' action = newEmptyMVar >>= \var -> | |
(forkIO (action >>= \r -> putMVar var r)) >> | |
(return $ Async var) | |
wait :: Async a -> IO a | |
wait (Async var) = readMVar var | |
-- how is this async? | |
blah = do | |
a1 <- async'' (getURL "http://www.wikipedia.org/wiki/Shovel") | |
a2 <- async'' (getURL "http://www.wikipedia.org/wiki/Spade") | |
r1 <- wait a1 | |
r2 <- wait a2 | |
print (B.length r1, B.length r2) | |
-- how is this async? I'm flatmapping ... it looks like I always need a1 to finish before a2? | |
blah' = async''(getURL "http://www.wikipedia.org/wiki/Shovel") >>= \a1 -> | |
async''(getURL "http://www.wikipedia.org/wiki/Spade") >>= \a2 -> | |
wait a1 >>= \r1 -> | |
wait a2 >>= \r2 -> | |
(print (B.length r1, B.length r2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment