Skip to content

Commit aaa7738

Browse files
committed
Merge pull request #13 from purescript/scan
Add scanl/r
2 parents df0327e + 24b377f commit aaa7738

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,8 @@
103103

104104
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
105105

106+
scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
107+
108+
scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
109+
106110
zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]

src/Data/Traversable.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Data.Traversable
44
, sequence
55
, for
66
, zipWithA
7+
, scanl
8+
, scanr
79
, mapAccumL
810
, mapAccumR
911
) where
@@ -67,6 +69,9 @@ instance applyStateL :: Apply (StateL s) where
6769
instance applicativeStateL :: Applicative (StateL s) where
6870
pure a = StateL $ \s -> Tuple s a
6971

72+
scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
73+
scanl f b0 xs = snd $ mapAccumL (\b a -> let b' = f b a in Tuple b' b') b0 xs
74+
7075
mapAccumL :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
7176
mapAccumL f s0 xs = stateL (traverse (\a -> StateL $ \s -> f s a) xs) s0
7277

@@ -87,6 +92,9 @@ instance applyStateR :: Apply (StateR s) where
8792
instance applicativeStateR :: Applicative (StateR s) where
8893
pure a = StateR $ \s -> Tuple s a
8994

95+
scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
96+
scanr f b0 xs = snd $ mapAccumR (\b a -> let b' = f a b in Tuple b' b') b0 xs
97+
9098
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
9199
mapAccumR f s0 xs = stateR (traverse (\a -> StateR $ \s -> f s a) xs) s0
92100

0 commit comments

Comments
 (0)