Created
July 18, 2017 13:29
-
-
Save edsko/5bc00065ad2104bdfa38296f3102e185 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
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