on master help sign in
Implement scanr1
afba704Bryan O'Sullivan
Data/Text.hs+7−4
@@ -77,7 +77,7 @@ module Data.Text
77 , scanl
77 , scanl
78 , scanl1
78 , scanl1
79 , scanr
79 , scanr
80− -- , scanr1
80+ , scanr1
81
81
82 -- ** Accumulating maps
82 -- ** Accumulating maps
83 , mapAccumL
83 , mapAccumL
@@ -520,9 +520,12 @@ scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
520scanr f z = S.reverse . S.reverseScanr f z . reverseStream
520scanr f z = S.reverse . S.reverseScanr f z . reverseStream
521{-# INLINE scanr #-}
521{-# INLINE scanr #-}
522
522
523−shorten n t@(S.Stream arr off len) | n < len && n > 0 = S.Stream arr off n
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
524− | otherwise = t
527+ | otherwise = scanr f (last t) (init t)
525−info (S.Stream _ _ len) = len
528+{-# INLINE scanr1 #-}
526
529
527-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
530-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
528-- function to each element of a 'Text', passing an accumulating
531-- 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
101prop_scanl f z = L.scanl f z `eqP` (unpack . T.scanl f z)
101prop_scanl f z = L.scanl f z `eqP` (unpack . T.scanl f z)
102prop_scanl1 f = L.scanl1 f `eqP` (unpack . T.scanl1 f)
102prop_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)
104+prop_scanr1 f = L.scanr1 f `eqP` (unpack . T.scanr1 f)
104
105
105prop_mapAccumL f z = (snd . L.mapAccumL f z)`eqP` (unpack . T.mapAccumL f z)
106prop_mapAccumL f z = (snd . L.mapAccumL f z)`eqP` (unpack . T.mapAccumL f z)
106 where types = f :: Int -> Char -> (Int,Char)
107 where types = f :: Int -> Char -> (Int,Char)
@@ -240,6 +241,7 @@ tests = [
240 ("prop_scanl", mytest prop_scanl),
241 ("prop_scanl", mytest prop_scanl),
241 ("prop_scanl1", mytest prop_scanl1),
242 ("prop_scanl1", mytest prop_scanl1),
242 ("prop_scanr", mytest prop_scanr),
243 ("prop_scanr", mytest prop_scanr),
244+ ("prop_scanr1", mytest prop_scanr1),
243
245
244 ("prop_mapAccumL", mytest prop_mapAccumL),
246 ("prop_mapAccumL", mytest prop_mapAccumL),
245
247