Created
December 1, 2011 23:15
-
-
Save bdonlan/1420607 to your computer and use it in GitHub Desktop.
This file contains 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
newtype BlockingLazyGenerator a = BLG (MVar (a, BlockingLazyGenerator a)) | |
pushBLG :: BlockingLazyGenerator a -> a -> IO (BlockingLazyGenerator a) | |
pushBLG (BLG blg) val = do | |
newBLG <- fmap BLG newEmptyMVar | |
res <- tryPutMVar blg (val, newBLG) | |
when (not res) $ error "tried to put a BLG more than once" | |
return newBLG | |
unsafeTailBLG :: BlockingLazyGenerator a -> [a] | |
unsafeTailBLG (BLG b) = unsafeInterleaveIO $ do | |
(v, rem) <- readMVar b | |
return (v:unsafeInterleaveIO rem) | |
newBLG :: IO (BlockingLazyGenerator a, [a]) | |
newBLG = do | |
blg <- fmap BLG newEmptyMVar | |
return (blg, unsafeTailBLG blg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment