Commits

Bryan O'Sullivan committed 15e5918

Implement and test lazy scanl and scanl1

Comments (0)

Files changed (2)

Data/Text/Lazy.hs

     -- * Construction
 
     -- ** Scans
-    -- , scanl
-    -- , scanl1
+    , scanl
+    , scanl1
     -- , scanr
     -- , scanr1
 
 minimum t = S.minimum (stream t)
 {-# INLINE minimum #-}
 
+-- | /O(n)/ 'scanl' is similar to 'foldl', but returns a list of
+-- successive reduced values from the left. This function is subject
+-- to array fusion.
+--
+-- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
+--
+-- Note that
+--
+-- > last (scanl f z xs) == foldl f z xs.
+scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
+scanl f z t = unstream (S.scanl f z (stream t))
+{-# INLINE scanl #-}
+
+-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
+-- value argument.  This function is subject to array fusion.
+--
+-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
+scanl1 :: (Char -> Char -> Char) -> Text -> Text
+scanl1 f t0 = case uncons t0 of
+                Nothing -> empty
+                Just (t,ts) -> scanl f t ts
+{-# INLINE scanl1 #-}
+
 -- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
 -- prefix of @t@ of length @n@, and whose second is the remainder of
 -- the string. It is equivalent to @('take' n t, 'drop' n t)@.

tests/Properties.hs

 prop_TL_minimum        = L.minimum     `eqEP` TL.minimum
 
 prop_T_scanl f z       = L.scanl f z   `eqP`  (unpackT . T.scanl f z)
+prop_TL_scanl f z      = L.scanl f z   `eqP`  (unpackT . TL.scanl f z)
 prop_T_scanl1 f        = L.scanl1 f    `eqP`  (unpackT . T.scanl1 f)
+prop_TL_scanl1 f       = L.scanl1 f    `eqP`  (unpackT . TL.scanl1 f)
 prop_T_scanr f z       = L.scanr f z   `eqP`  (unpackT . T.scanr f z)
 prop_T_scanr1 f        = L.scanr1 f    `eqP`  (unpackT . T.scanr1 f)
 
   ("prop_TL_minimum", mytest prop_TL_minimum),
 
   ("prop_T_scanl", mytest prop_T_scanl),
+  ("prop_TL_scanl", mytest prop_TL_scanl),
   ("prop_T_scanl1", mytest prop_T_scanl1),
+  ("prop_TL_scanl1", mytest prop_TL_scanl1),
   ("prop_T_scanr", mytest prop_T_scanr),
   ("prop_T_scanr1", mytest prop_T_scanr1),