on master help sign in
Implement mapAccumR.
044dff4Bryan O'Sullivan
Data/Text.hs+12−1
@@ -81,7 +81,7 @@ module Data.Text
81
81
82 -- ** Accumulating maps
82 -- ** Accumulating maps
83 , mapAccumL
83 , mapAccumL
84− -- , mapAccumR
84+ , mapAccumR
85
85
86 -- ** Generation and unfolding
86 -- ** Generation and unfolding
87 , replicate
87 , replicate
@@ -538,6 +538,17 @@ mapAccumL f s t = case uncons t of
538 where (s', y ) = f s x
538 where (s', y ) = f s x
539 (s'',ys) = mapAccumL f s' xs
539 (s'',ys) = mapAccumL f s' xs
540
540
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+
541-- -----------------------------------------------------------------------------
552-- -----------------------------------------------------------------------------
542-- ** Generating and unfolding 'Text's
553-- ** Generating and unfolding 'Text's
543
554
tests/Properties.hs+3
@@ -105,6 +105,8 @@ prop_scanr1 f = L.scanr1 f `eqP` (unpack . T.scanr1 f)
105
105
106prop_mapAccumL f z = L.mapAccumL f z `eqP` (second unpack . T.mapAccumL f z)
106prop_mapAccumL f z = L.mapAccumL f z `eqP` (second unpack . T.mapAccumL f z)
107 where types = f :: Int -> Char -> (Int,Char)
107 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)
108
110
109prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)
111prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)
110prop_unfoldr n = L.unfoldr f `eq` (unpack . T.unfoldr f)
112prop_unfoldr n = L.unfoldr f `eq` (unpack . T.unfoldr f)
@@ -244,6 +246,7 @@ tests = [
244 ("prop_scanr1", mytest prop_scanr1),
246 ("prop_scanr1", mytest prop_scanr1),
245
247
246 ("prop_mapAccumL", mytest prop_mapAccumL),
248 ("prop_mapAccumL", mytest prop_mapAccumL),
249+ ("prop_mapAccumR", mytest prop_mapAccumR),
247
250
248 ("prop_replicate", mytest prop_replicate),
251 ("prop_replicate", mytest prop_replicate),
249 ("prop_unfoldr", mytest prop_unfoldr),
252 ("prop_unfoldr", mytest prop_unfoldr),