Skip to content

Instantly share code, notes, and snippets.

@bdonlan
Created December 1, 2011 23:15
Show Gist options
  • Save bdonlan/1420607 to your computer and use it in GitHub Desktop.
Save bdonlan/1420607 to your computer and use it in GitHub Desktop.
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