on master help sign in
Implement and test lazy scanr1
1fde6e9Bryan O'Sullivan
Data/Text/Lazy.hs+7−1
@@ -82,7 +82,7 @@ module Data.Text.Lazy
82 , scanl
82 , scanl
83 , scanl1
83 , scanl1
84 , scanr
84 , scanr
85− -- , scanr1
85+ , scanr1
86
86
87 -- ** Accumulating maps
87 -- ** Accumulating maps
88 , mapAccumL
88 , mapAccumL
@@ -478,6 +478,12 @@ scanl1 f t0 = case uncons t0 of
478scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
478scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
479scanr f v = reverse . scanl (flip f) v . reverse
479scanr f v = reverse . scanl (flip f) v . reverse
480
480
481+-- | /O(n)/ 'scanr1' is a variant of 'scanr' that has no starting
482+-- value argument.
483+scanr1 :: (Char -> Char -> Char) -> Text -> Text
484+scanr1 f t | null t = empty
485+ | otherwise = scanr f (last t) (init t)
486+
481-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
487-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
482-- function to each element of a 'Text', passing an accumulating
488-- function to each element of a 'Text', passing an accumulating
483-- parameter from left to right, and returns a final 'Text'.
489-- parameter from left to right, and returns a final 'Text'.
tests/Properties.hs+2
@@ -206,6 +206,7 @@ prop_TL_scanl1 f = L.scanl1 f `eqP` (unpackT . TL.scanl1 f)
206prop_T_scanr f z = L.scanr f z `eqP` (unpackT . T.scanr f z)
206prop_T_scanr f z = L.scanr f z `eqP` (unpackT . T.scanr f z)
207prop_TL_scanr f z = L.scanr f z `eqP` (unpackT . TL.scanr f z)
207prop_TL_scanr f z = L.scanr f z `eqP` (unpackT . TL.scanr f z)
208prop_T_scanr1 f = L.scanr1 f `eqP` (unpackT . T.scanr1 f)
208prop_T_scanr1 f = L.scanr1 f `eqP` (unpackT . T.scanr1 f)
209+prop_TL_scanr1 f = L.scanr1 f `eqP` (unpackT . TL.scanr1 f)
209
210
210prop_T_mapAccumL f z = L.mapAccumL f z `eqP` (second unpackT . T.mapAccumL f z)
211prop_T_mapAccumL f z = L.mapAccumL f z `eqP` (second unpackT . T.mapAccumL f z)
211 where types = f :: Int -> Char -> (Int,Char)
212 where types = f :: Int -> Char -> (Int,Char)
@@ -435,6 +436,7 @@ tests = [
435 ("prop_T_scanr", mytest prop_T_scanr),
436 ("prop_T_scanr", mytest prop_T_scanr),
436 ("prop_TL_scanr", mytest prop_TL_scanr),
437 ("prop_TL_scanr", mytest prop_TL_scanr),
437 ("prop_T_scanr1", mytest prop_T_scanr1),
438 ("prop_T_scanr1", mytest prop_T_scanr1),
439+ ("prop_TL_scanr1", mytest prop_TL_scanr1),
438
440
439 ("prop_T_mapAccumL", mytest prop_T_mapAccumL),
441 ("prop_T_mapAccumL", mytest prop_T_mapAccumL),
440 ("prop_TL_mapAccumL", mytest prop_TL_mapAccumL),
442 ("prop_TL_mapAccumL", mytest prop_TL_mapAccumL),