Skip to content

Instantly share code, notes, and snippets.

@michaelt michaelt/oneShot.hs
Created Oct 7, 2016

Embed
What would you like to do?
sink to run with the await oneshot rule
{-#LANGUAGE BangPatterns #-}
import Pipes
import qualified Pipes.Prelude as P
import Control.Monad.Trans.State.Strict
import GHC.Magic
import qualified Pipes.Internal as I
import Data.IORef
import Control.Monad
emit :: Int -> Int -> IORef Int -> Producer Int IO ()
emit !large !m ref = do
n <- liftIO (readIORef ref)
when (n < large) $ yield n >> emit large (m+1) ref
receive :: IORef Int -> Consumer Int IO ()
receive ref = do
n <- await
liftIO $ writeIORef ref $! (n+1::Int)
receive ref
main = do
ref <- newIORef 0
let b = receive ref
large = 10000000 :: Int
larger = 2*large
runEffect $ emit large 0 ref >-> b
readIORef ref >>= print
runEffect $ emit larger 0 ref >-> b
readIORef ref >>= print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.