on master help sign in
Rewrite mapAccumR in terms of mapAccumL
8a4c1caBryan O'Sullivan
Data/Text.hs+5−9
@@ -789,16 +789,12 @@ mapAccumL f z0 = S.mapAccumL f z0 . stream
789{-# INLINE mapAccumL #-}
789{-# INLINE mapAccumL #-}
790
790
791-- | The 'mapAccumR' function behaves like a combination of 'map' and
791-- | The 'mapAccumR' function behaves like a combination of 'map' and
792−-- 'foldr'; it applies a function to each element of a 'Text', passing
792+-- a strict 'foldr'; it applies a function to each element of a
793−-- an accumulating parameter from right to left, and returning a final
793+-- 'Text', passing an accumulating parameter from right to left, and
794−-- value of this accumulator together with the new 'Text'.
794+-- returning a final value of this accumulator together with the new
795+-- 'Text'.
795mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
796mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
796−mapAccumR f = go
797+mapAccumR f z0 = second reverse . S.mapAccumL f z0 . reverseStream
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
802{-# INLINE mapAccumR #-}
798{-# INLINE mapAccumR #-}
803
799
804-- -----------------------------------------------------------------------------
800-- -----------------------------------------------------------------------------