on master help sign in
Reimplement mapAccumL properly.
d3c8aeaBryan O'Sullivan
Data/Text.hs+6−6
@@ -530,12 +530,12 @@ scanr1 f t | null t = empty
530-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
530-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
531-- function to each element of a 'Text', passing an accumulating
531-- function to each element of a 'Text', passing an accumulating
532-- parameter from left to right, and returns a final 'Text'.
532-- parameter from left to right, and returns a final 'Text'.
533−--
534−-- /Note/: Unlike the version over lists, this function does not return a
535−-- final value for the accumulator.
536−mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> Text
533+mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
537−mapAccumL f z t = unstream (S.mapAccumL f z (stream t))
534+mapAccumL f s t = case uncons t of
535+ Nothing -> (s, empty)
536+ Just (x, xs) -> (s'', cons y ys)
537+ where (s', y ) = f s x
538−{-# INLINE mapAccumL #-}
538+ (s'',ys) = mapAccumL f s' xs
539
539
540-- -----------------------------------------------------------------------------
540-- -----------------------------------------------------------------------------
541-- ** Generating and unfolding 'Text's
541-- ** Generating and unfolding 'Text's
Data/Text/Fusion.hs+8−1
@@ -602,7 +602,14 @@ reverseScanr f z0 (Stream next0 s0 len) = Stream next (S1 :!: z0 :!: s0) (len+1)
602-- -----------------------------------------------------------------------------
602-- -----------------------------------------------------------------------------
603-- ** Accumulating maps
603-- ** Accumulating maps
604
604
605+-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
606+-- function to each element of a stream, passing an accumulating
607+-- parameter from left to right, and returns a final stream.
608+--
609+-- /Note/: Unlike the version over lists, this function does not
610+-- return a final value for the accumulator, because the nature of
611+-- streams precludes it.
605−mapAccumL :: (a -> Char -> (a,Char)) -> a -> Stream Char -> Stream Char
612+mapAccumL :: (a -> b -> (a,b)) -> a -> Stream b -> Stream b
606mapAccumL f z0 (Stream next0 s0 len) = Stream next (s0 :!: z0) len
613mapAccumL f z0 (Stream next0 s0 len) = Stream next (s0 :!: z0) len
607 where
614 where
608 {-# INLINE next #-}
615 {-# INLINE next #-}
tests/Properties.hs+1−1
@@ -103,7 +103,7 @@ prop_scanl1 f = L.scanl1 f `eqP` (unpack . T.scanl1 f)
103prop_scanr f z = L.scanr f z `eqP` (unpack . T.scanr f z)
103prop_scanr f z = L.scanr f z `eqP` (unpack . T.scanr f z)
104prop_scanr1 f = L.scanr1 f `eqP` (unpack . T.scanr1 f)
104prop_scanr1 f = L.scanr1 f `eqP` (unpack . T.scanr1 f)
105
105
106−prop_mapAccumL f z = (snd . L.mapAccumL f z)`eqP` (unpack . T.mapAccumL f z)
106+prop_mapAccumL f z = L.mapAccumL f z `eqP` (second unpack . T.mapAccumL f z)
107 where types = f :: Int -> Char -> (Int,Char)
107 where types = f :: Int -> Char -> (Int,Char)
108
108
109prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)
109prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)