Commits

Bryan O'Sullivan committed 284cb87

Implement scanr1

Comments (0)

Files changed (2)

     , scanl
     , scanl1
     , scanr
-    -- , scanr1
+    , scanr1
 
     -- ** Accumulating maps
     , mapAccumL
 scanr f z = S.reverse . S.reverseScanr f z . reverseStream
 {-# INLINE scanr #-}
 
-shorten n t@(S.Stream arr off len) | n < len && n > 0 = S.Stream arr off n
-                                   | otherwise        = t
-info (S.Stream _ _ len) = len
+-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
+-- value argument.  This function is subject to array fusion.
+scanr1 :: (Char -> Char -> Char) -> Text -> Text
+scanr1 f t | null t    = empty
+           | otherwise = scanr f (last t) (init t)
+{-# INLINE scanr1 #-}
 
 -- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
 -- function to each element of a 'Text', passing an accumulating

tests/Properties.hs

 prop_scanl f z       = L.scanl f z   `eqP`  (unpack . T.scanl f z)
 prop_scanl1 f        = L.scanl1 f    `eqP`  (unpack . T.scanl1 f)
 prop_scanr f z       = L.scanr f z   `eqP`  (unpack . T.scanr f z)
+prop_scanr1 f        = L.scanr1 f    `eqP`  (unpack . T.scanr1 f)
 
 prop_mapAccumL f z   = (snd . L.mapAccumL f z)`eqP` (unpack . T.mapAccumL f z)
     where types = f :: Int -> Char -> (Int,Char)
   ("prop_scanl", mytest prop_scanl),
   ("prop_scanl1", mytest prop_scanl1),
   ("prop_scanr", mytest prop_scanr),
+  ("prop_scanr1", mytest prop_scanr1),
 
   ("prop_mapAccumL", mytest prop_mapAccumL),