Bryan O'Sullivan committed 6ad9ad1

Implement mapAccumR.

Comments (0)

Files changed (2)

     -- ** Accumulating maps
     , mapAccumL
-    -- , mapAccumR
+    , mapAccumR
     -- ** Generation and unfolding
     , replicate
                         where (s', y ) = f s x
                               (s'',ys) = mapAccumL f s' xs
+-- | The 'mapAccumR' function behaves like a combination of 'map' and
+-- 'foldr'; it applies a function to each element of a 'Text', passing
+-- an accumulating parameter from right to left, and returning a final
+-- value of this accumulator together with the new 'Text'.
+mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
+mapAccumR f s t = case uncons t of
+                    Nothing -> (s, empty)
+                    Just (x, xs) ->  (s'', cons y ys)
+                        where (s'',y ) = f s' x
+                              (s', ys) = mapAccumR f s xs
 -- -----------------------------------------------------------------------------
 -- ** Generating and unfolding 'Text's


 prop_mapAccumL f z   = L.mapAccumL f z `eqP` (second unpack . T.mapAccumL f z)
     where types = f :: Int -> Char -> (Int,Char)
+prop_mapAccumR f z   = L.mapAccumR f z `eqP` (second unpack . T.mapAccumR f z)
+    where types = f :: Int -> Char -> (Int,Char)
 prop_replicate n     = L.replicate n `eq`   (unpack . T.replicate n)
 prop_unfoldr n       = L.unfoldr f   `eq`   (unpack . T.unfoldr f)
   ("prop_scanr1", mytest prop_scanr1),
   ("prop_mapAccumL", mytest prop_mapAccumL),
+  ("prop_mapAccumR", mytest prop_mapAccumR),
   ("prop_replicate", mytest prop_replicate),
   ("prop_unfoldr", mytest prop_unfoldr),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.