Created
October 11, 2016 20:29
-
-
Save oshyshko/b0858ca49aed50980d72329393456da5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module RWSMonads where | |
import Control.Monad.Reader (Reader, runReader, ask) | |
import Control.Monad.State (State, runState, evalState, get, put) -- also gets/modify | |
import Control.Monad.Writer (Writer, runWriter, tell) | |
getSetCount :: Reader String Int | |
getSetCount = do | |
endpoint <- ask | |
return $ length endpoint | |
doStuffWithLogging :: String -> Writer String () | |
doStuffWithLogging endpoint = do | |
tell $ "hello " ++ endpoint ++ "\n" | |
tell "asdhasdfhgasfg\n" | |
return () | |
-- String -- just an argument to a fn (no part of State stuff) | |
-- Int -- state that you can access with get/put | |
-- Bool -- what function returns | |
doStuffWithState :: String -> State Int Bool | |
doStuffWithState arg = do | |
s <- get | |
put (s + length arg) | |
return True | |
main :: IO () | |
main = do | |
let r = runReader getSetCount "http://api.factual.com/sets" | |
let (r, w) = runWriter (doStuffWithLogging "http://api.factual.com/sets") | |
let (r, s) = runState (doStuffWithState "someArg") 12345 -- return result and state | |
let r = evalState (doStuffWithState "someArg") 12345 -- return result only | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment