on master help sign in
Implement and test lazy scanr and reverse

--HG-- extra : convert_revision : af450f27ecc5d40ed62e1d944aaeee4bbda71f9d

commit1a7653a64119e40ae678f7dd626285851fe5461f authorBryan O'Sullivan <bos@serpentine.com> dateSun 8 Mar 2009, 01:26 (18 years ago) parentf2565d2
3 files changed+19−3
Data/Text.hs+1−1 Data/Text/Lazy.hs+14−2 tests/Properties.hs+4
Data/Text.hs+1−1
@@ -500,7 +500,7 @@ scanl1 f t | null t = empty
500500
501501-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'.
502502--
503−-- > scanr f v t == reverse (scanl (flip f) v t)
503+-- > scanr f v == reverse . scanl (flip f) v . reverse
504504scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
505505scanr f z = S.reverse . S.reverseScanr f z . reverseStream
506506{-# INLINE scanr #-}
Data/Text/Lazy.hs+14−2
@@ -58,7 +58,7 @@ module Data.Text.Lazy
5858 , intercalate
5959 , intersperse
6060 , transpose
61− -- , reverse
61+ , reverse
6262
6363 -- * Folds
6464 , foldl
@@ -81,7 +81,7 @@ module Data.Text.Lazy
8181 -- ** Scans
8282 , scanl
8383 , scanl1
84− -- , scanr
84+ , scanr
8585 -- , scanr1
8686
8787 -- ** Accumulating maps
@@ -363,6 +363,12 @@ transpose ts = L.map (\ss -> Chunk (T.pack ss) Empty)
363363 (L.transpose (L.map unpack ts))
364364-- TODO: make this fast
365365
366+-- | /O(n)/ 'reverse' @t@ returns the elements of @t@ in reverse order.
367+reverse :: Text -> Text
368+reverse = rev Empty
369+ where rev a Empty = a
370+ rev a (Chunk t ts) = rev (Chunk (T.reverse t) a) ts
371+
366372-- | /O(n)/ 'foldl', applied to a binary operator, a starting value
367373-- (typically the left-identity of the operator), and a 'Text',
368374-- reduces the 'Text' using the binary operator, from left to right.
@@ -466,6 +472,12 @@ scanl1 f t0 = case uncons t0 of
466472 Just (t,ts) -> scanl f t ts
467473{-# INLINE scanl1 #-}
468474
475+-- | /O(n)/ 'scanr' is the right-to-left dual of 'scanl'.
476+--
477+-- > scanr f v == reverse . scanl (flip f) v . reverse
478+scanr :: (Char -> Char -> Char) -> Char -> Text -> Text
479+scanr f v = reverse . scanl (flip f) v . reverse
480+
469481-- | /O(n)/ Like a combination of 'map' and 'foldl'. Applies a
470482-- function to each element of a 'Text', passing an accumulating
471483-- parameter from left to right, and returns a final 'Text'.
tests/Properties.hs+4
@@ -159,6 +159,7 @@ prop_TL_intersperse c = L.intersperse c `eqP` (unpackT . TL.intersperse c)
159159prop_T_transpose = L.transpose `eq` (map unpackT . T.transpose . map T.pack)
160160prop_TL_transpose = L.transpose `eq` (map unpackT . TL.transpose . map TL.pack)
161161prop_T_reverse = L.reverse `eqP` (unpackT . T.reverse)
162+prop_TL_reverse = L.reverse `eqP` (unpackT . TL.reverse)
162163prop_T_reverse_short n = L.reverse `eqP` (unpackT . S.reverse . shorten n . S.stream)
163164
164165prop_T_foldl f z = L.foldl f z `eqP` (T.foldl f z)
@@ -198,6 +199,7 @@ prop_TL_scanl f z = L.scanl f z `eqP` (unpackT . TL.scanl f z)
198199prop_T_scanl1 f = L.scanl1 f `eqP` (unpackT . T.scanl1 f)
199200prop_TL_scanl1 f = L.scanl1 f `eqP` (unpackT . TL.scanl1 f)
200201prop_T_scanr f z = L.scanr f z `eqP` (unpackT . T.scanr f z)
202+prop_TL_scanr f z = L.scanr f z `eqP` (unpackT . TL.scanr f z)
201203prop_T_scanr1 f = L.scanr1 f `eqP` (unpackT . T.scanr1 f)
202204
203205prop_T_mapAccumL f z = L.mapAccumL f z `eqP` (second unpackT . T.mapAccumL f z)
@@ -392,6 +394,7 @@ tests = [
392394 ("prop_T_transpose", mytest prop_T_transpose),
393395 ("prop_TL_transpose", mytest prop_TL_transpose),
394396 ("prop_T_reverse", mytest prop_T_reverse),
397+ ("prop_TL_reverse", mytest prop_TL_reverse),
395398 ("prop_T_reverse_short", mytest prop_T_reverse_short),
396399
397400 ("prop_T_foldl", mytest prop_T_foldl),
@@ -425,6 +428,7 @@ tests = [
425428 ("prop_T_scanl1", mytest prop_T_scanl1),
426429 ("prop_TL_scanl1", mytest prop_TL_scanl1),
427430 ("prop_T_scanr", mytest prop_T_scanr),
431+ ("prop_TL_scanr", mytest prop_TL_scanr),
428432 ("prop_T_scanr1", mytest prop_T_scanr1),
429433
430434 ("prop_T_mapAccumL", mytest prop_T_mapAccumL),