Skip to content

Commit 0a0c372

Browse files
committed
Adding RWS tests
1 parent 2d3efee commit 0a0c372

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

src/Control/Monad/RWS/Trans.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ instance monadWriterRWST :: (Monad m, Monoid w) => MonadWriter w (RWST r w s m)
9595
instance monadRWSRWST :: (Monad m, Monoid w) => MonadRWS r w s (RWST r w s m)
9696

9797
instance monadRecRWST :: (Monoid w, MonadRec m) => MonadRec (RWST r w s m) where
98-
tailRecM k a = RWST \r s -> tailRecM k' { reader: r, writer: mempty, state: s, result: a }
98+
tailRecM k a = RWST \r s -> tailRecM (k' r) { writer: mempty, state: s, result: a }
9999
where
100-
k' o = do
101-
see <- runRWST (k o.result) o.reader o.state
100+
k' r o = do
101+
see <- runRWST (k o.result) r o.state
102102
return case see.result of
103-
Left a -> Left { reader: o.reader, state: see.state, result: a, writer: o.writer <> see.log }
103+
Left a -> Left { state: see.state, result: a, writer: o.writer <> see.log }
104104
Right b -> Right (mkSee see.state b (o.writer <> see.log))

test/Example/RWS.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// module Example.RWS
2+
3+
exports.t = function(){
4+
return new Date().valueOf();
5+
};

test/Example/RWS.purs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Example.RWS where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff
6+
import Control.Monad.Eff.Console
7+
import Control.Monad.RWS
8+
import Control.Monad.RWS.Trans
9+
import Control.Monad.Rec.Class
10+
import Control.Monad.State
11+
import Control.Monad.Writer
12+
13+
import Data.Either
14+
import Data.Identity
15+
16+
loop :: Int -> RWST String (Array String) Int Identity Unit
17+
loop n = tailRecM go n
18+
where
19+
go 0 = do
20+
tell [ "Done!" ]
21+
return (Right unit)
22+
go n = do
23+
x <- get
24+
put (x + 1)
25+
return (Left (n - 1))
26+
27+
main = do
28+
t1 <- t
29+
res <- pure $ runIdentity (runRWST (loop 10000) "" 0)
30+
t2 <- t
31+
print $ "RWST.state: " ++ show res.state
32+
print $ "RWST.log: " ++ show res.log
33+
print $ "t2 - t1 = " ++ show (t2 - t1)
34+
35+
foreign import t :: forall eff. Eff eff Number

test/Test/Main.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ main = do
99
Example.State.main
1010
Example.StateEff.main
1111
Example.Writer.main
12+
Example.RWS.main

0 commit comments

Comments
 (0)