on master help sign in
Implement mapAccumR.

--HG-- extra : convert_revision : 845d33352bb095f1ac197c9ac75991f29f6c2b38

commit044dff47d535720ac659188f8f72b0bbfe6129e5 authorBryan O'Sullivan <bos@serpentine.com> dateSun 22 Feb 2009, 23:02 (18 years ago) parent7a617d4
2 files changed+15−1
Data/Text.hs+12−1 tests/Properties.hs+3
Data/Text.hs+12−1
@@ -81,7 +81,7 @@ module Data.Text
8181
8282 -- ** Accumulating maps
8383 , mapAccumL
84− -- , mapAccumR
84+ , mapAccumR
8585
8686 -- ** Generation and unfolding
8787 , replicate
@@ -538,6 +538,17 @@ mapAccumL f s t = case uncons t of
538538 where (s', y ) = f s x
539539 (s'',ys) = mapAccumL f s' xs
540540
541+-- | The 'mapAccumR' function behaves like a combination of 'map' and
542+-- 'foldr'; it applies a function to each element of a 'Text', passing
543+-- an accumulating parameter from right to left, and returning a final
544+-- value of this accumulator together with the new 'Text'.
545+mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
546+mapAccumR f s t = case uncons t of
547+ Nothing -> (s, empty)
548+ Just (x, xs) -> (s'', cons y ys)
549+ where (s'',y ) = f s' x
550+ (s', ys) = mapAccumR f s xs
551+
541552-- -----------------------------------------------------------------------------
542553-- ** Generating and unfolding 'Text's
543554
tests/Properties.hs+3
@@ -105,6 +105,8 @@ prop_scanr1 f = L.scanr1 f `eqP` (unpack . T.scanr1 f)
105105
106106prop_mapAccumL f z = L.mapAccumL f z `eqP` (second unpack . T.mapAccumL f z)
107107 where types = f :: Int -> Char -> (Int,Char)
108+prop_mapAccumR f z = L.mapAccumR f z `eqP` (second unpack . T.mapAccumR f z)
109+ where types = f :: Int -> Char -> (Int,Char)
108110
109111prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)
110112prop_unfoldr n = L.unfoldr f `eq` (unpack . T.unfoldr f)
@@ -244,6 +246,7 @@ tests = [
244246 ("prop_scanr1", mytest prop_scanr1),
245247
246248 ("prop_mapAccumL", mytest prop_mapAccumL),
249+ ("prop_mapAccumR", mytest prop_mapAccumR),
247250
248251 ("prop_replicate", mytest prop_replicate),
249252 ("prop_unfoldr", mytest prop_unfoldr),