Skip to content

Commit

Permalink
Merge pull request #13 from purescript/scan
Browse files Browse the repository at this point in the history
Add scanl/r
  • Loading branch information
garyb committed Dec 18, 2014
2 parents df0327e + 24b377f commit aaa7738
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@

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

scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b

scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b

zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
8 changes: 8 additions & 0 deletions src/Data/Traversable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Data.Traversable
, sequence
, for
, zipWithA
, scanl
, scanr
, mapAccumL
, mapAccumR
) where
Expand Down Expand Up @@ -67,6 +69,9 @@ instance applyStateL :: Apply (StateL s) where
instance applicativeStateL :: Applicative (StateL s) where
pure a = StateL $ \s -> Tuple s a

scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
scanl f b0 xs = snd $ mapAccumL (\b a -> let b' = f b a in Tuple b' b') b0 xs

mapAccumL :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
mapAccumL f s0 xs = stateL (traverse (\a -> StateL $ \s -> f s a) xs) s0

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

scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
scanr f b0 xs = snd $ mapAccumR (\b a -> let b' = f a b in Tuple b' b') b0 xs

mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
mapAccumR f s0 xs = stateR (traverse (\a -> StateR $ \s -> f s a) xs) s0

0 comments on commit aaa7738

Please sign in to comment.