on master help sign in
Reimplement mapAccumL properly.

--HG-- extra : convert_revision : 2a34b8e5ab564cf9025a78a27378ae75e543940d

commitd3c8aeacd0387d2642ecf7814eeb88115db2845b authorBryan O'Sullivan <bos@serpentine.com> dateSun 22 Feb 2009, 22:56 (18 years ago) parentafba704
3 files changed+15−8
Data/Text.hs+6−6 Data/Text/Fusion.hs+8−1 tests/Properties.hs+1−1
Data/Text.hs+6−6
@@ -530,12 +530,12 @@ scanr1 f t | null t = empty
530530-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
531531-- function to each element of a 'Text', passing an accumulating
532532-- 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
537−mapAccumL f z t = unstream (S.mapAccumL f z (stream t))
538−{-# INLINE mapAccumL #-}
533+mapAccumL :: (a -> Char -> (a,Char)) -> a -> Text -> (a, Text)
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+ (s'',ys) = mapAccumL f s' xs
539539
540540-- -----------------------------------------------------------------------------
541541-- ** 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)
602602-- -----------------------------------------------------------------------------
603603-- ** Accumulating maps
604604
605−mapAccumL :: (a -> Char -> (a,Char)) -> a -> Stream Char -> Stream Char
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.
612+mapAccumL :: (a -> b -> (a,b)) -> a -> Stream b -> Stream b
606613mapAccumL f z0 (Stream next0 s0 len) = Stream next (s0 :!: z0) len
607614 where
608615 {-# INLINE next #-}
tests/Properties.hs+1−1
@@ -103,7 +103,7 @@ prop_scanl1 f = L.scanl1 f `eqP` (unpack . T.scanl1 f)
103103prop_scanr f z = L.scanr f z `eqP` (unpack . T.scanr f z)
104104prop_scanr1 f = L.scanr1 f `eqP` (unpack . T.scanr1 f)
105105
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)
107107 where types = f :: Int -> Char -> (Int,Char)
108108
109109prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)