Skip to content

Commit 40f5bed

Browse files
committed
Add mapAccumL/R
1 parent a39165c commit 40f5bed

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

Gruntfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function(grunt) {
1313

1414
pscMake: ["<%=libFiles%>"],
1515
dotPsci: ["<%=libFiles%>"],
16-
docgen: {
16+
pscDocs: {
1717
readme: {
1818
src: "src/**/*.purs",
1919
dest: "README.md"
@@ -25,6 +25,6 @@ module.exports = function(grunt) {
2525
grunt.loadNpmTasks("grunt-contrib-clean");
2626
grunt.loadNpmTasks("grunt-purescript");
2727

28-
grunt.registerTask("make", ["pscMake", "dotPsci", "docgen"]);
28+
grunt.registerTask("make", ["pscMake", "dotPsci", "pscDocs"]);
2929
grunt.registerTask("default", ["make"]);
3030
};

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464

6565
## Module Data.Traversable
6666

67+
### Types
68+
69+
newtype StateL s a where
70+
StateL :: (s -> Tuple s a) -> StateL s a
71+
72+
newtype StateR s a where
73+
StateR :: (s -> Tuple s a) -> StateR s a
74+
75+
6776
### Type Classes
6877

6978
class (Functor t, Foldable t) <= Traversable t where
@@ -73,6 +82,18 @@
7382

7483
### Type Class Instances
7584

85+
instance applicativeStateL :: Applicative (StateL s)
86+
87+
instance applicativeStateR :: Applicative (StateR s)
88+
89+
instance applyStateL :: Apply (StateL s)
90+
91+
instance applyStateR :: Apply (StateR s)
92+
93+
instance functorStateL :: Functor (StateL s)
94+
95+
instance functorStateR :: Functor (StateR s)
96+
7697
instance traversableArray :: Traversable Prim.Array
7798

7899
instance traversableEither :: Traversable (Either a)
@@ -88,4 +109,12 @@
88109

89110
for :: forall a b m t. (Applicative m, Traversable t) => t a -> (a -> m b) -> m (t b)
90111

112+
mapAccumL :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
113+
114+
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
115+
116+
stateL :: forall s a. StateL s a -> s -> Tuple s a
117+
118+
stateR :: forall s a. StateR s a -> s -> Tuple s a
119+
91120
zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"dependencies": {
44
"grunt": "~0.4.4",
5-
"grunt-purescript": "~0.5.1",
5+
"grunt-purescript": "~0.6.0",
66
"grunt-contrib-clean": "~0.5.0"
77
}
88
}

src/Data/Traversable.purs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,43 @@ for x f = traverse f x
4949
zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
5050
zipWithA f xs ys = sequence (zipWith f xs ys)
5151

52+
newtype StateL s a = StateL (s -> Tuple s a)
53+
54+
stateL :: forall s a. StateL s a -> s -> Tuple s a
55+
stateL (StateL k) = k
56+
57+
instance functorStateL :: Functor (StateL s) where
58+
(<$>) f k = StateL $ \s -> case stateL k s of
59+
Tuple s1 a -> Tuple s1 (f a)
60+
61+
instance applyStateL :: Apply (StateL s) where
62+
(<*>) f x = StateL $ \s -> case stateL f s of
63+
Tuple s1 f' -> case stateL x s1 of
64+
Tuple s2 x' -> Tuple s2 (f' x')
65+
66+
instance applicativeStateL :: Applicative (StateL s) where
67+
pure a = StateL $ \s -> Tuple s a
68+
69+
mapAccumL :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
70+
mapAccumL f s0 xs = stateL (traverse (\a -> StateL $ \s -> f s a) xs) s0
71+
72+
newtype StateR s a = StateR (s -> Tuple s a)
73+
74+
stateR :: forall s a. StateR s a -> s -> Tuple s a
75+
stateR (StateR k) = k
76+
77+
instance functorStateR :: Functor (StateR s) where
78+
(<$>) f k = StateR $ \s -> case stateR k s of
79+
Tuple s1 a -> Tuple s1 (f a)
80+
81+
instance applyStateR :: Apply (StateR s) where
82+
(<*>) f x = StateR $ \s -> case stateR x s of
83+
Tuple s1 x' -> case stateR f s1 of
84+
Tuple s2 f' -> Tuple s2 (f' x')
85+
86+
instance applicativeStateR :: Applicative (StateR s) where
87+
pure a = StateR $ \s -> Tuple s a
88+
89+
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
90+
mapAccumR f s0 xs = stateR (traverse (\a -> StateR $ \s -> f s a) xs) s0
91+

0 commit comments

Comments
 (0)