Commits

Bryan O'Sullivan  committed cbe7667

Implement and test lazy special folds

  • Participants
  • Parent commits 13eb150

Comments (0)

Files changed (2)

File Data/Text/Lazy.hs

     , foldr1
 
     -- ** Special folds
-    -- , concat
-    -- , concatMap
-    -- , any
-    -- , all
-    -- , maximum
-    -- , minimum
+    , concat
+    , concatMap
+    , any
+    , all
+    , maximum
+    , minimum
 
     -- * Construction
 
 foldr1 f t = S.foldr1 f (stream t)
 {-# INLINE foldr1 #-}
 
+-- | /O(n)/ Concatenate a list of 'Text's. Subject to array fusion.
+concat :: [Text] -> Text
+concat ts = unstream (S.concat (L.map stream ts))
+{-# INLINE concat #-}
+
+-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
+-- concatenate the results.  This function is subject to array fusion.
+--
+-- Note: if in 'concatMap' @f@ @t@, @f@ is defined in terms of fusible
+-- functions, it will also be fusible.
+concatMap :: (Char -> Text) -> Text -> Text
+concatMap f t = unstream (S.concatMap (stream . f) (stream t))
+{-# INLINE concatMap #-}
+
+-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
+-- 'Text' @t@ satisifes the predicate @p@. Subject to array fusion.
+any :: (Char -> Bool) -> Text -> Bool
+any p t = S.any p (stream t)
+{-# INLINE any #-}
+
+-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
+-- 'Text' @t@ satisify the predicate @p@. Subject to array fusion.
+all :: (Char -> Bool) -> Text -> Bool
+all p t = S.all p (stream t)
+{-# INLINE all #-}
+
+-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
+-- must be non-empty. Subject to array fusion.
+maximum :: Text -> Char
+maximum t = S.maximum (stream t)
+{-# INLINE maximum #-}
+
+-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
+-- must be non-empty. Subject to array fusion.
+minimum :: Text -> Char
+minimum t = S.minimum (stream t)
+{-# INLINE minimum #-}
+
 -- | /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_TL_foldr1 f       = L.foldr1 f   `eqEP` TL.foldr1 f
 
 prop_T_concat          = L.concat      `eq`   (unpackT . T.concat . map T.pack)
+prop_TL_concat         = L.concat      `eq`   (unpackT . TL.concat . map TL.pack)
 prop_T_concatMap f     = L.concatMap f `eqP`  (unpackT . T.concatMap (T.pack . f))
+prop_TL_concatMap f    = L.concatMap f `eqP`  (unpackT . TL.concatMap (TL.pack . f))
 prop_T_any p           = L.any p       `eqP`  T.any p
+prop_TL_any p          = L.any p       `eqP`  TL.any p
 prop_T_all p           = L.all p       `eqP`  T.all p
+prop_TL_all p          = L.all p       `eqP`  TL.all p
 prop_T_maximum         = L.maximum     `eqEP` T.maximum
+prop_TL_maximum        = L.maximum     `eqEP` TL.maximum
 prop_T_minimum         = L.minimum     `eqEP` T.minimum
+prop_TL_minimum        = L.minimum     `eqEP` TL.minimum
 
 prop_T_scanl f z       = L.scanl f z   `eqP`  (unpackT . T.scanl f z)
 prop_T_scanl1 f        = L.scanl1 f    `eqP`  (unpackT . T.scanl1 f)
   ("prop_TL_foldr1", mytest prop_TL_foldr1),
 
   ("prop_T_concat", mytest prop_T_concat),
+  ("prop_TL_concat", mytest prop_TL_concat),
   ("prop_T_concatMap", mytest prop_T_concatMap),
+  ("prop_TL_concatMap", mytest prop_TL_concatMap),
   ("prop_T_any", mytest prop_T_any),
+  ("prop_TL_any", mytest prop_TL_any),
   ("prop_T_all", mytest prop_T_all),
+  ("prop_TL_all", mytest prop_TL_all),
   ("prop_T_maximum", mytest prop_T_maximum),
+  ("prop_TL_maximum", mytest prop_TL_maximum),
   ("prop_T_minimum", mytest prop_T_minimum),
+  ("prop_TL_minimum", mytest prop_TL_minimum),
 
   ("prop_T_scanl", mytest prop_T_scanl),
   ("prop_T_scanl1", mytest prop_T_scanl1),