Skip to content

@mmakowski /logger.hs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
module Logger where
import System.IO
type Logger = String -> IO ()
data Logging a = Logging { unLogging :: Logger -> IO a }
logLine :: String -> Logging ()
logLine s = Logging $ \l -> l s
runLogging :: Logging a -> IO ()
runLogging (Logging logging) = do
log <- openFile "log.txt" WriteMode
logging $ hPutStrLn log
hClose log
(>>>=) :: Logging a -> (a -> Logging b) -> Logging b
(Logging l1) >>>= f = Logging $ \l -> l1 l >>= \x -> unLogging (f x) l
wrap :: a -> Logging a
wrap x = Logging $ \_ -> return x
instance Monad Logging where
return = wrap
(>>=) = (>>>=)
-- even better, we can just use standard monad functions, or even do syntax:
main :: IO ()
main = runLogging doSomething
doSomething :: Logging ()
doSomething = do
logLine "Hello Again"
x <- calcSomething "1234"
logLine (show x)
calcSomething :: String -> Logging Int
calcSomething s = do
logLine "calculating length"
return $ length s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.