Skip to content

Commit 24b377f

Browse files
committed
Add scanl/r
1 parent 7cb7edb commit 24b377f

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
@@ -107,4 +107,8 @@
107107

108108
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
109109

110+
scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
111+
112+
scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
113+
110114
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
@@ -74,6 +76,9 @@ instance applyStateL :: Apply (StateL s) where
7476
instance applicativeStateL :: Applicative (StateL s) where
7577
pure a = StateL $ \s -> Tuple s a
7678

79+
scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
80+
scanl f b0 xs = snd $ mapAccumL (\b a -> let b' = f b a in Tuple b' b') b0 xs
81+
7782
mapAccumL :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
7883
mapAccumL f s0 xs = stateL (traverse (\a -> StateL $ \s -> f s a) xs) s0
7984

@@ -94,6 +99,9 @@ instance applyStateR :: Apply (StateR s) where
9499
instance applicativeStateR :: Applicative (StateR s) where
95100
pure a = StateR $ \s -> Tuple s a
96101

102+
scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
103+
scanr f b0 xs = snd $ mapAccumR (\b a -> let b' = f a b in Tuple b' b') b0 xs
104+
97105
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
98106
mapAccumR f s0 xs = stateR (traverse (\a -> StateR $ \s -> f s a) xs) s0
99107

0 commit comments

Comments
 (0)