Skip to content

Commit 7cb7edb

Browse files
committed
Merge pull request #11 from purescript/mapAccum
Add mapAccumL/R
2 parents a39165c + be13aff commit 7cb7edb

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464

6565
## Module Data.Traversable
6666

67+
### Types
68+
69+
6770
### Type Classes
6871

6972
class (Functor t, Foldable t) <= Traversable t where
@@ -73,6 +76,18 @@
7376

7477
### Type Class Instances
7578

79+
instance applicativeStateL :: Applicative (StateL s)
80+
81+
instance applicativeStateR :: Applicative (StateR s)
82+
83+
instance applyStateL :: Apply (StateL s)
84+
85+
instance applyStateR :: Apply (StateR s)
86+
87+
instance functorStateL :: Functor (StateL s)
88+
89+
instance functorStateR :: Functor (StateR s)
90+
7691
instance traversableArray :: Traversable Prim.Array
7792

7893
instance traversableEither :: Traversable (Either a)
@@ -88,4 +103,8 @@
88103

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

106+
mapAccumL :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
107+
108+
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
109+
91110
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: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
module Data.Traversable where
1+
module Data.Traversable
2+
( Traversable
3+
, traverse
4+
, sequence
5+
, for
6+
, zipWithA
7+
, mapAccumL
8+
, mapAccumR
9+
) where
210

311
import Prelude
412
import Data.Array (zipWith)
@@ -49,3 +57,43 @@ for x f = traverse f x
4957
zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
5058
zipWithA f xs ys = sequence (zipWith f xs ys)
5159

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

0 commit comments

Comments
 (0)