on master help sign in
Implement and test accumulating maps

--HG-- extra : convert_revision : b5be2635d7e835afe7f1e7de5c3a8eba854c8e22

commit6cd41122976bde19fc728e57779a373681532ec8 authorBryan O'Sullivan <bos@serpentine.com> dateTue 3 Mar 2009, 06:37 (18 years ago) parent75c0703
2 files changed+29−2
Data/Text/Lazy.hs+23−2 tests/Properties.hs+6
Data/Text/Lazy.hs+23−2
@@ -83,8 +83,8 @@ module Data.Text.Lazy
8383 -- , scanr1
8484
8585 -- ** Accumulating maps
86− -- , mapAccumL
87− -- , mapAccumR
86+ , mapAccumL
87+ , mapAccumR
8888
8989 -- ** Generation and unfolding
9090 -- , replicate
@@ -447,6 +447,27 @@ scanl1 f t0 = case uncons t0 of
447447 Just (t,ts) -> scanl f t ts
448448{-# INLINE scanl1 #-}
449449
450+-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
451+-- function to each element of a 'Text', passing an accumulating
452+-- parameter from left to right, and returns a final 'Text'.
453+mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
454+mapAccumL f s t = case uncons t of
455+ Nothing -> (s, empty)
456+ Just (x, xs) -> (s'', cons y ys)
457+ where (s', y ) = f s x
458+ (s'',ys) = mapAccumL f s' xs
459+
460+-- | The 'mapAccumR' function behaves like a combination of 'map' and
461+-- 'foldr'; it applies a function to each element of a 'Text', passing
462+-- an accumulating parameter from right to left, and returning a final
463+-- value of this accumulator together with the new 'Text'.
464+mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
465+mapAccumR f s t = case uncons t of
466+ Nothing -> (s, empty)
467+ Just (x, xs) -> (s'', cons y ys)
468+ where (s'',y ) = f s' x
469+ (s', ys) = mapAccumR f s xs
470+
450471-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
451472-- prefix of @t@ of length @n@, and whose second is the remainder of
452473-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
tests/Properties.hs+6
@@ -191,8 +191,12 @@ prop_T_scanr1 f = L.scanr1 f `eqP` (unpackT . T.scanr1 f)
191191
192192prop_T_mapAccumL f z = L.mapAccumL f z `eqP` (second unpackT . T.mapAccumL f z)
193193 where types = f :: Int -> Char -> (Int,Char)
194+prop_TL_mapAccumL f z = L.mapAccumL f z `eqP` (second unpackT . TL.mapAccumL f z)
195+ where types = f :: Int -> Char -> (Int,Char)
194196prop_T_mapAccumR f z = L.mapAccumR f z `eqP` (second unpackT . T.mapAccumR f z)
195197 where types = f :: Int -> Char -> (Int,Char)
198+prop_TL_mapAccumR f z = L.mapAccumR f z `eqP` (second unpackT . TL.mapAccumR f z)
199+ where types = f :: Int -> Char -> (Int,Char)
196200
197201prop_T_replicate n = L.replicate n `eq` (unpackT . T.replicate n)
198202prop_T_unfoldr n = L.unfoldr f `eq` (unpackT . T.unfoldr f)
@@ -389,7 +393,9 @@ tests = [
389393 ("prop_T_scanr1", mytest prop_T_scanr1),
390394
391395 ("prop_T_mapAccumL", mytest prop_T_mapAccumL),
396+ ("prop_TL_mapAccumL", mytest prop_TL_mapAccumL),
392397 ("prop_T_mapAccumR", mytest prop_T_mapAccumR),
398+ ("prop_TL_mapAccumR", mytest prop_TL_mapAccumR),
393399
394400 ("prop_T_replicate", mytest prop_T_replicate),
395401 ("prop_T_unfoldr", mytest prop_T_unfoldr),