Commits

Bryan O'Sullivan committed 13eb150

Implement and test lazy folds

  • Participants
  • Parent commits dbcbc8a

Comments (0)

Files changed (2)

File Data/Text/Lazy.hs

     -- , reverse
 
     -- * Folds
-    -- , foldl
-    -- , foldl'
-    -- , foldl1
-    -- , foldl1'
-    -- , foldr
-    -- , foldr1
+    , foldl
+    , foldl'
+    , foldl1
+    , foldl1'
+    , foldr
+    , foldr1
 
     -- ** Special folds
     -- , concat
 intersperse c t = unstream (S.intersperse c (stream t))
 {-# INLINE intersperse #-}
 
+-- | /O(n)/ 'foldl', applied to a binary operator, a starting value
+-- (typically the left-identity of the operator), and a 'Text',
+-- reduces the 'Text' using the binary operator, from left to right.
+-- Subject to array fusion.
+foldl :: (b -> Char -> b) -> b -> Text -> b
+foldl f z t = S.foldl f z (stream t)
+{-# INLINE foldl #-}
+
+-- | /O(n)/ A strict version of 'foldl'.
+-- Subject to array fusion.
+foldl' :: (b -> Char -> b) -> b -> Text -> b
+foldl' f z t = S.foldl' f z (stream t)
+{-# INLINE foldl' #-}
+
+-- | /O(n)/ A variant of 'foldl' that has no starting value argument,
+-- and thus must be applied to a non-empty 'Text'.  Subject to array
+-- fusion.
+foldl1 :: (Char -> Char -> Char) -> Text -> Char
+foldl1 f t = S.foldl1 f (stream t)
+{-# INLINE foldl1 #-}
+
+-- | /O(n)/ A strict version of 'foldl1'.
+-- Subject to array fusion.
+foldl1' :: (Char -> Char -> Char) -> Text -> Char
+foldl1' f t = S.foldl1' f (stream t)
+{-# INLINE foldl1' #-}
+
+-- | /O(n)/ 'foldr', applied to a binary operator, a starting value
+-- (typically the right-identity of the operator), and a 'Text',
+-- reduces the 'Text' using the binary operator, from right to left.
+-- Subject to array fusion.
+foldr :: (Char -> b -> b) -> b -> Text -> b
+foldr f z t = S.foldr f z (stream t)
+{-# INLINE foldr #-}
+
+-- | /O(n)/ A variant of 'foldr' that has no starting value argument, and
+-- thust must be applied to a non-empty 'Text'.  Subject to array
+-- fusion.
+foldr1 :: (Char -> Char -> Char) -> Text -> Char
+foldr1 f t = S.foldr1 f (stream t)
+{-# INLINE foldr1 #-}
+
 -- | /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)@.

File tests/Properties.hs

 
 prop_T_foldl f z       = L.foldl f z  `eqP`  (T.foldl f z)
     where types      = f :: Char -> Char -> Char
+prop_TL_foldl f z      = L.foldl f z  `eqP`  (TL.foldl f z)
+    where types      = f :: Char -> Char -> Char
 prop_T_foldl' f z      = L.foldl' f z `eqP`  T.foldl' f z
     where types      = f :: Char -> Char -> Char
+prop_TL_foldl' f z     = L.foldl' f z `eqP`  TL.foldl' f z
+    where types      = f :: Char -> Char -> Char
 prop_T_foldl1 f        = L.foldl1 f   `eqEP` T.foldl1 f
+prop_TL_foldl1 f       = L.foldl1 f   `eqEP` TL.foldl1 f
 prop_T_foldl1' f       = L.foldl1' f  `eqEP` T.foldl1' f
+prop_TL_foldl1' f      = L.foldl1' f  `eqEP` TL.foldl1' f
 prop_T_foldr f z       = L.foldr f z  `eqP`  T.foldr f z
     where types      = f :: Char -> Char -> Char
+prop_TL_foldr f z      = L.foldr f z  `eqP`  TL.foldr f z
+    where types      = f :: Char -> Char -> Char
 prop_T_foldr1 f        = L.foldr1 f   `eqEP` T.foldr1 f
+prop_TL_foldr1 f       = L.foldr1 f   `eqEP` TL.foldr1 f
 
 prop_T_concat          = L.concat      `eq`   (unpackT . T.concat . map T.pack)
 prop_T_concatMap f     = L.concatMap f `eqP`  (unpackT . T.concatMap (T.pack . f))
   ("prop_T_reverse_short", mytest prop_T_reverse_short),
 
   ("prop_T_foldl", mytest prop_T_foldl),
+  ("prop_TL_foldl", mytest prop_TL_foldl),
   ("prop_T_foldl'", mytest prop_T_foldl'),
+  ("prop_TL_foldl'", mytest prop_TL_foldl'),
   ("prop_T_foldl1", mytest prop_T_foldl1),
+  ("prop_TL_foldl1", mytest prop_TL_foldl1),
   ("prop_T_foldl1'", mytest prop_T_foldl1'),
+  ("prop_TL_foldl1'", mytest prop_TL_foldl1'),
   ("prop_T_foldr", mytest prop_T_foldr),
+  ("prop_TL_foldr", mytest prop_TL_foldr),
   ("prop_T_foldr1", mytest prop_T_foldr1),
+  ("prop_TL_foldr1", mytest prop_TL_foldr1),
 
   ("prop_T_concat", mytest prop_T_concat),
   ("prop_T_concatMap", mytest prop_T_concatMap),