Skip to content

Instantly share code, notes, and snippets.

@edsko
Created July 18, 2017 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edsko/5bc00065ad2104bdfa38296f3102e185 to your computer and use it in GitHub Desktop.
Save edsko/5bc00065ad2104bdfa38296f3102e185 to your computer and use it in GitHub Desktop.
import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import System.IO.Unsafe
newtype LazyIO a = LazyIO { runLazyIO :: IO a }
instance Monad LazyIO where
return a = LazyIO (return a)
LazyIO x >>= f = LazyIO $ unsafeInterleaveIO $ x >>= runLazyIO . f
instance Applicative LazyIO where
pure = return
(<*>) = ap
instance Functor LazyIO where
fmap = liftM
instance MonadIO LazyIO where
liftIO = LazyIO
-- try
--
-- > runLazyIO simpleExample >>= print
simpleExample :: LazyIO ()
simpleExample = do
a <- liftIO (print "computing a") >> return 1
b <- liftIO (print "computing b") >> return 2
liftIO $ print $ a + b
-- try
--
-- > runLazyIO hereWeGo >>= print
hereWeGo :: LazyIO ()
hereWeGo = do
xs <- mapM (\x -> liftIO (print x) >> return x) [1..]
liftIO (print (take 10 xs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment