Created
August 23, 2015 08:59
-
-
Save yoshitsugu/ff0d7a17a44628348674 to your computer and use it in GitHub Desktop.
reader sample code
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.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