on master help sign in
Implement scanr1

--HG-- extra : convert_revision : ca8100e2e4aa76aa6c1115af58bd1e9171a392f5

commitafba704f2c1d03d67844f3e83f9dec868da26789 authorBryan O'Sullivan <bos@serpentine.com> dateSun 22 Feb 2009, 09:13 (18 years ago) parente2f1397
2 files changed+9−4
Data/Text.hs+7−4 tests/Properties.hs+2
Data/Text.hs+7−4
@@ -77,7 +77,7 @@ module Data.Text
7777 , scanl
7878 , scanl1
7979 , scanr
80− -- , scanr1
80+ , scanr1
8181
8282 -- ** Accumulating maps
8383 , mapAccumL
@@ -520,9 +520,12 @@ scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
520520scanr f z = S.reverse . S.reverseScanr f z . reverseStream
521521{-# INLINE scanr #-}
522522
523−shorten n t@(S.Stream arr off len) | n < len && n > 0 = S.Stream arr off n
524− | otherwise = t
525−info (S.Stream _ _ len) = len
523+-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
524+-- value argument. This function is subject to array fusion.
525+scanr1 :: (Char -> Char -> Char) -> Text -> Text
526+scanr1 f t | null t = empty
527+ | otherwise = scanr f (last t) (init t)
528+{-# INLINE scanr1 #-}
526529
527530-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
528531-- function to each element of a 'Text', passing an accumulating
tests/Properties.hs+2
@@ -101,6 +101,7 @@ prop_minimum = L.minimum `eqEP` T.minimum
101101prop_scanl f z = L.scanl f z `eqP` (unpack . T.scanl f z)
102102prop_scanl1 f = L.scanl1 f `eqP` (unpack . T.scanl1 f)
103103prop_scanr f z = L.scanr f z `eqP` (unpack . T.scanr f z)
104+prop_scanr1 f = L.scanr1 f `eqP` (unpack . T.scanr1 f)
104105
105106prop_mapAccumL f z = (snd . L.mapAccumL f z)`eqP` (unpack . T.mapAccumL f z)
106107 where types = f :: Int -> Char -> (Int,Char)
@@ -240,6 +241,7 @@ tests = [
240241 ("prop_scanl", mytest prop_scanl),
241242 ("prop_scanl1", mytest prop_scanl1),
242243 ("prop_scanr", mytest prop_scanr),
244+ ("prop_scanr1", mytest prop_scanr1),
243245
244246 ("prop_mapAccumL", mytest prop_mapAccumL),
245247