Skip to content

Commit f5bf8b1

Browse files
authored
add MyRW monad (#27)
1 parent 72f1b1c commit f5bf8b1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

MonadsAndEffects/3.3/myrw.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module MyRw where
2+
3+
import Control.Monad.Trans.Reader
4+
import Control.Monad.Trans.Writer
5+
import Control.Monad.Trans (lift)
6+
import Data.Char (toUpper)
7+
8+
9+
type MyRW = ReaderT [String] (Writer String)
10+
11+
12+
myAsks :: ([String] -> a) -> MyRW a
13+
myAsks = asks
14+
15+
16+
myTell :: String -> MyRW ()
17+
myTell = lift . tell
18+
19+
20+
logFirstAndRetSecond :: MyRW String
21+
logFirstAndRetSecond = do
22+
el1 <- myAsks head
23+
el2 <- myAsks (map toUpper . head . tail)
24+
myTell el1
25+
return el2
26+
27+
28+
runMyRW :: MyRW a -> [String] -> (a, String)
29+
runMyRW rw e = runWriter (runReaderT rw e)

0 commit comments

Comments
 (0)