on master help sign in
Implement and test lazy scanr1

--HG-- extra : convert_revision : e8d8aaaaabd7d7fddecba64abfd1c42990abcd7f

commit1fde6e94164315aa94f4d245a92f25d43564c36f authorBryan O'Sullivan <bos@serpentine.com> dateSun 8 Mar 2009, 01:30 (18 years ago) parentddbaab6
2 files changed+9−1
Data/Text/Lazy.hs+7−1 tests/Properties.hs+2
Data/Text/Lazy.hs+7−1
@@ -82,7 +82,7 @@ module Data.Text.Lazy
8282 , scanl
8383 , scanl1
8484 , scanr
85− -- , scanr1
85+ , scanr1
8686
8787 -- ** Accumulating maps
8888 , mapAccumL
@@ -478,6 +478,12 @@ scanl1 f t0 = case uncons t0 of
478478scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
479479scanr f v = reverse . scanl (flip f) v . reverse
480480
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+
481487-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
482488-- function to each element of a 'Text', passing an accumulating
483489-- 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)
206206prop_T_scanr f z = L.scanr f z `eqP` (unpackT . T.scanr f z)
207207prop_TL_scanr f z = L.scanr f z `eqP` (unpackT . TL.scanr f z)
208208prop_T_scanr1 f = L.scanr1 f `eqP` (unpackT . T.scanr1 f)
209+prop_TL_scanr1 f = L.scanr1 f `eqP` (unpackT . TL.scanr1 f)
209210
210211prop_T_mapAccumL f z = L.mapAccumL f z `eqP` (second unpackT . T.mapAccumL f z)
211212 where types = f :: Int -> Char -> (Int,Char)
@@ -435,6 +436,7 @@ tests = [
435436 ("prop_T_scanr", mytest prop_T_scanr),
436437 ("prop_TL_scanr", mytest prop_TL_scanr),
437438 ("prop_T_scanr1", mytest prop_T_scanr1),
439+ ("prop_TL_scanr1", mytest prop_TL_scanr1),
438440
439441 ("prop_T_mapAccumL", mytest prop_T_mapAccumL),
440442 ("prop_TL_mapAccumL", mytest prop_TL_mapAccumL),