Skip to content

Commit 9e7a233

Browse files
committed
Merge pull request #39 from owickstrom/master
Add StateT with Eff example
2 parents b64544b + 1d1cdab commit 9e7a233

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Gruntfile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ module.exports = function(grunt) {
7676
src: ["examples/State.purs", "<%=libFiles%>"],
7777
dest: "tmp/State.js"
7878
},
79+
exampleStateEff: {
80+
src: ["examples/StateEff.purs", "<%=libFiles%>"],
81+
dest: "tmp/StateEff.js"
82+
},
7983
exampleWriter: {
8084
src: ["examples/Writer.purs", "<%=libFiles%>"],
8185
dest: "tmp/Writer.js"
@@ -93,6 +97,9 @@ module.exports = function(grunt) {
9397
exampleState: {
9498
src: "tmp/State.js"
9599
},
100+
exampleStateEff: {
101+
src: "tmp/StateEff.js"
102+
},
96103
exampleWriter: {
97104
src: "tmp/Writer.js"
98105
},
@@ -109,6 +116,7 @@ module.exports = function(grunt) {
109116

110117
grunt.registerTask("exampleReader", ["psc:exampleReader", "execute:exampleReader"]);
111118
grunt.registerTask("exampleState", ["psc:exampleState", "execute:exampleState"]);
119+
grunt.registerTask("exampleStateEff", ["psc:exampleStateEff", "execute:exampleStateEff"]);
112120
grunt.registerTask("exampleWriter", ["psc:exampleWriter", "execute:exampleWriter"]);
113121
grunt.registerTask("exampleCont", ["psc:exampleCont", "execute:exampleCont"]);
114122
grunt.registerTask("examples", ["psc", "execute"]);

examples/StateEff.purs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Main where
2+
3+
import Control.Monad.State
4+
import Control.Monad.State.Class
5+
import Control.Monad.State.Trans
6+
import Control.Monad.Eff
7+
import Control.Monad.Trans
8+
import Data.Tuple
9+
import Debug.Trace
10+
11+
type Stack r t = StateT [Number] (Eff r) t
12+
13+
pop :: forall r. Stack (trace :: Trace | r) Number
14+
pop = do
15+
(x:xs) <- get
16+
lift $ trace $ "Popping " ++ show x
17+
put xs
18+
return x
19+
20+
push :: forall r. Number -> Stack (trace :: Trace | r) Unit
21+
push x = do
22+
lift $ trace $ "Pushing " ++ show x
23+
modify $ (:) x
24+
return unit
25+
26+
testState :: forall r. Stack (trace :: Trace | r) Number
27+
testState = do
28+
push 1
29+
push 2
30+
push 3
31+
pop
32+
pop
33+
34+
main = do
35+
result <- runStateT testState []
36+
case result of
37+
Tuple value state -> do
38+
print $ "state: " ++ (show state)
39+
print $ "value: " ++ (show value)

0 commit comments

Comments
 (0)