Skip to content

Instantly share code, notes, and snippets.

@yoshitsugu
Created August 23, 2015 08:59
Show Gist options
  • Save yoshitsugu/ff0d7a17a44628348674 to your computer and use it in GitHub Desktop.
Save yoshitsugu/ff0d7a17a44628348674 to your computer and use it in GitHub Desktop.
reader sample code
import Control.Monad.Reader
data Config = Config { verbose :: Int, debug :: Bool }
configToLevel :: Config -> Int
configToLevel config
| debug config = 10
| otherwise = verbose config
outputLevel :: Reader Config [Int]
outputLevel = do
config <- ask
return [1..(configToLevel config)]
output :: Int -> String -> Reader Config (Maybe String)
output level str = do
levels <- outputLevel
return $ if elem level levels then Just str else Nothing
-- *Main> runReader (output 5 "hoge") (Config 1 True)
-- Just "hoge"
-- *Main> runReader (output 5 "hoge") (Config 1 False)
-- Nothing
-- *Main> runReader (output 5 "hoge") (Config 2 False)
-- Nothing
-- *Main> runReader (output 5 "hoge") (Config 6 False)
-- Just "hoge"
-- *Main> runReader (output 5 "hoge") (Config 5 False)
-- Just "hoge"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment