on master help sign in
Implement and test lazy scanl and scanl1

--HG-- extra : convert_revision : d0156e1f742959519247c8ac923c746546b8a263

commit75c07032ee014f87ef6d721c88f00dd32e4d373c authorBryan O'Sullivan <bos@serpentine.com> dateTue 3 Mar 2009, 00:53 (18 years ago) parent7b8d093
2 files changed+29−2
Data/Text/Lazy.hs+25−2 tests/Properties.hs+4
Data/Text/Lazy.hs+25−2
@@ -77,8 +77,8 @@ module Data.Text.Lazy
7777 -- * Construction
7878
7979 -- ** Scans
80− -- , scanl
81− -- , scanl1
80+ , scanl
81+ , scanl1
8282 -- , scanr
8383 -- , scanr1
8484
@@ -424,6 +424,29 @@ minimum :: Text -> Char
424424minimum t = S.minimum (stream t)
425425{-# INLINE minimum #-}
426426
427+-- | /O(n)/ 'scanl' is similar to 'foldl', but returns a list of
428+-- successive reduced values from the left. This function is subject
429+-- to array fusion.
430+--
431+-- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
432+--
433+-- Note that
434+--
435+-- > last (scanl f z xs) == foldl f z xs.
436+scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
437+scanl f z t = unstream (S.scanl f z (stream t))
438+{-# INLINE scanl #-}
439+
440+-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
441+-- value argument. This function is subject to array fusion.
442+--
443+-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
444+scanl1 :: (Char -> Char -> Char) -> Text -> Text
445+scanl1 f t0 = case uncons t0 of
446+ Nothing -> empty
447+ Just (t,ts) -> scanl f t ts
448+{-# INLINE scanl1 #-}
449+
427450-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
428451-- prefix of @t@ of length @n@, and whose second is the remainder of
429452-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
tests/Properties.hs+4
@@ -183,7 +183,9 @@ prop_T_minimum = L.minimum `eqEP` T.minimum
183183prop_TL_minimum = L.minimum `eqEP` TL.minimum
184184
185185prop_T_scanl f z = L.scanl f z `eqP` (unpackT . T.scanl f z)
186+prop_TL_scanl f z = L.scanl f z `eqP` (unpackT . TL.scanl f z)
186187prop_T_scanl1 f = L.scanl1 f `eqP` (unpackT . T.scanl1 f)
188+prop_TL_scanl1 f = L.scanl1 f `eqP` (unpackT . TL.scanl1 f)
187189prop_T_scanr f z = L.scanr f z `eqP` (unpackT . T.scanr f z)
188190prop_T_scanr1 f = L.scanr1 f `eqP` (unpackT . T.scanr1 f)
189191
@@ -380,7 +382,9 @@ tests = [
380382 ("prop_TL_minimum", mytest prop_TL_minimum),
381383
382384 ("prop_T_scanl", mytest prop_T_scanl),
385+ ("prop_TL_scanl", mytest prop_TL_scanl),
383386 ("prop_T_scanl1", mytest prop_T_scanl1),
387+ ("prop_TL_scanl1", mytest prop_TL_scanl1),
384388 ("prop_T_scanr", mytest prop_T_scanr),
385389 ("prop_T_scanr1", mytest prop_T_scanr1),
386390