on master help sign in
Fix up lazy mapAccumL and mapAccumR
e4a3dd6Bryan O'Sullivan
Data/Text/Lazy.hs+15−14
@@ -692,29 +692,30 @@ scanr1 :: (Char -> Char -> Char) -> Text -> Text
692scanr1 f t | null t = empty
692scanr1 f t | null t = empty
693 | otherwise = scanr f (last t) (init t)
693 | otherwise = scanr f (last t) (init t)
694
694
695−-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
695+-- | /O(n)/ Like a combination of 'map' and 'foldl''. Applies a
696-- function to each element of a 'Text', passing an accumulating
696-- function to each element of a 'Text', passing an accumulating
697-- parameter from left to right, and returns a final 'Text'.
697-- parameter from left to right, and returns a final 'Text'.
698mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
698mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
699mapAccumL f = go
699mapAccumL f = go
700− where go s t = case uncons t of
700+ where
701− Nothing -> (s, empty)
701+ go z (Chunk c cs) = (z'', Chunk c' cs')
702− Just (x, xs) -> (s'', cons y ys)
703− where (s', y ) = f s x
702+ where (z', c') = T.mapAccumL f z c
704− (s'',ys) = go s' xs
703+ (z'', cs') = go z' cs
704+ go z Empty = (z, Empty)
705{-# INLINE mapAccumL #-}
705{-# INLINE mapAccumL #-}
706
706
707-- | The 'mapAccumR' function behaves like a combination of 'map' and
707-- | The 'mapAccumR' function behaves like a combination of 'map' and
708−-- 'foldr'; it applies a function to each element of a 'Text', passing
708+-- a strict 'foldr'; it applies a function to each element of a
709−-- an accumulating parameter from right to left, and returning a final
709+-- 'Text', passing an accumulating parameter from right to left, and
710−-- value of this accumulator together with the new 'Text'.
710+-- returning a final value of this accumulator together with the new
711+-- 'Text'.
711mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
712mapAccumR :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
712mapAccumR f = go
713mapAccumR f = go
713− where go s t = case uncons t of
714+ where
714− Nothing -> (s, empty)
715+ go z (Chunk c cs) = (z'', Chunk c' cs')
715− Just (x, xs) -> (s'', cons y ys)
716− where (s'',y ) = f s' x
716+ where (z'', c') = T.mapAccumR f z' c
717− (s', ys) = go s xs
717+ (z', cs') = go z cs
718+ go z Empty = (z, Empty)
718{-# INLINE mapAccumR #-}
719{-# INLINE mapAccumR #-}
719
720
720-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
721-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input