on master help sign in
Rewrite mapAccumR in terms of mapAccumL

The docs now note that the function that mutates the accumulator is applied strictly. This was always true, but the lack of explicitness could have misled someone who'd assume behaviour similar to lists. --HG-- extra : convert_revision : d01452c8ad33b852da4b912a04e463bf0ffb0130

commit8a4c1ca5cb72596bc13cebcbfc219bae2e9b0535 authorBryan O'Sullivan <bos@serpentine.com> dateMon 13 Sep 2010, 00:39 (16 years ago) parent3e619a5
1 file changed+5−9
Data/Text.hs+5−9
Data/Text.hs+5−9
@@ -789,16 +789,12 @@ mapAccumL f z0 = S.mapAccumL f z0 . stream
789789{-# INLINE mapAccumL #-}
790790
791791-- | The 'mapAccumR' function behaves like a combination of 'map' and
792−-- 'foldr'; it applies a function to each element of a 'Text', passing
793−-- an accumulating parameter from right to left, and returning a final
794−-- value of this accumulator together with the new 'Text'.
792+-- a strict 'foldr'; it applies a function to each element of a
793+-- 'Text', passing an accumulating parameter from right to left, and
794+-- returning a final value of this accumulator together with the new
795+-- 'Text'.
795796mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
796−mapAccumR f = go
797− where go s t = case uncons t of
798− Nothing -> (s, empty)
799− Just (x, xs) -> (s'', cons y ys)
800− where (s'',y ) = f s' x
801− (s', ys) = go s xs
797+mapAccumR f z0 = second reverse . S.mapAccumL f z0 . reverseStream
802798{-# INLINE mapAccumR #-}
803799
804800-- -----------------------------------------------------------------------------