on master help sign in
Implement and test lazy special folds

--HG-- extra : convert_revision : e2ec5f4eb7d6335c1c42e061b2032d1e24963150

commit7b8d0930eae6a46eacdfd1d7ffefa9027d226903 authorBryan O'Sullivan <bos@serpentine.com> dateTue 3 Mar 2009, 00:49 (18 years ago) parentd44f3b0
2 files changed+56−6
Data/Text/Lazy.hs+44−6 tests/Properties.hs+12
Data/Text/Lazy.hs+44−6
@@ -67,12 +67,12 @@ module Data.Text.Lazy
6767 , foldr1
6868
6969 -- ** Special folds
70− -- , concat
71− -- , concatMap
72− -- , any
73− -- , all
74− -- , maximum
75− -- , minimum
70+ , concat
71+ , concatMap
72+ , any
73+ , all
74+ , maximum
75+ , minimum
7676
7777 -- * Construction
7878
@@ -386,6 +386,44 @@ foldr1 :: (Char -> Char -> Char) -> Text -> Char
386386foldr1 f t = S.foldr1 f (stream t)
387387{-# INLINE foldr1 #-}
388388
389+-- | /O(n)/ Concatenate a list of 'Text's. Subject to array fusion.
390+concat :: [Text] -> Text
391+concat ts = unstream (S.concat (L.map stream ts))
392+{-# INLINE concat #-}
393+
394+-- | /O(n)/ Map a function over a 'Text' that results in a 'Text', and
395+-- concatenate the results. This function is subject to array fusion.
396+--
397+-- Note: if in 'concatMap' @f@ @t@, @f@ is defined in terms of fusible
398+-- functions, it will also be fusible.
399+concatMap :: (Char -> Text) -> Text -> Text
400+concatMap f t = unstream (S.concatMap (stream . f) (stream t))
401+{-# INLINE concatMap #-}
402+
403+-- | /O(n)/ 'any' @p@ @t@ determines whether any character in the
404+-- 'Text' @t@ satisifes the predicate @p@. Subject to array fusion.
405+any :: (Char -> Bool) -> Text -> Bool
406+any p t = S.any p (stream t)
407+{-# INLINE any #-}
408+
409+-- | /O(n)/ 'all' @p@ @t@ determines whether all characters in the
410+-- 'Text' @t@ satisify the predicate @p@. Subject to array fusion.
411+all :: (Char -> Bool) -> Text -> Bool
412+all p t = S.all p (stream t)
413+{-# INLINE all #-}
414+
415+-- | /O(n)/ 'maximum' returns the maximum value from a 'Text', which
416+-- must be non-empty. Subject to array fusion.
417+maximum :: Text -> Char
418+maximum t = S.maximum (stream t)
419+{-# INLINE maximum #-}
420+
421+-- | /O(n)/ 'minimum' returns the minimum value from a 'Text', which
422+-- must be non-empty. Subject to array fusion.
423+minimum :: Text -> Char
424+minimum t = S.minimum (stream t)
425+{-# INLINE minimum #-}
426+
389427-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
390428-- prefix of @t@ of length @n@, and whose second is the remainder of
391429-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
tests/Properties.hs+12
@@ -170,11 +170,17 @@ prop_T_foldr1 f = L.foldr1 f `eqEP` T.foldr1 f
170170prop_TL_foldr1 f = L.foldr1 f `eqEP` TL.foldr1 f
171171
172172prop_T_concat = L.concat `eq` (unpackT . T.concat . map T.pack)
173+prop_TL_concat = L.concat `eq` (unpackT . TL.concat . map TL.pack)
173174prop_T_concatMap f = L.concatMap f `eqP` (unpackT . T.concatMap (T.pack . f))
175+prop_TL_concatMap f = L.concatMap f `eqP` (unpackT . TL.concatMap (TL.pack . f))
174176prop_T_any p = L.any p `eqP` T.any p
177+prop_TL_any p = L.any p `eqP` TL.any p
175178prop_T_all p = L.all p `eqP` T.all p
179+prop_TL_all p = L.all p `eqP` TL.all p
176180prop_T_maximum = L.maximum `eqEP` T.maximum
181+prop_TL_maximum = L.maximum `eqEP` TL.maximum
177182prop_T_minimum = L.minimum `eqEP` T.minimum
183+prop_TL_minimum = L.minimum `eqEP` TL.minimum
178184
179185prop_T_scanl f z = L.scanl f z `eqP` (unpackT . T.scanl f z)
180186prop_T_scanl1 f = L.scanl1 f `eqP` (unpackT . T.scanl1 f)
@@ -361,11 +367,17 @@ tests = [
361367 ("prop_TL_foldr1", mytest prop_TL_foldr1),
362368
363369 ("prop_T_concat", mytest prop_T_concat),
370+ ("prop_TL_concat", mytest prop_TL_concat),
364371 ("prop_T_concatMap", mytest prop_T_concatMap),
372+ ("prop_TL_concatMap", mytest prop_TL_concatMap),
365373 ("prop_T_any", mytest prop_T_any),
374+ ("prop_TL_any", mytest prop_TL_any),
366375 ("prop_T_all", mytest prop_T_all),
376+ ("prop_TL_all", mytest prop_TL_all),
367377 ("prop_T_maximum", mytest prop_T_maximum),
378+ ("prop_TL_maximum", mytest prop_TL_maximum),
368379 ("prop_T_minimum", mytest prop_T_minimum),
380+ ("prop_TL_minimum", mytest prop_TL_minimum),
369381
370382 ("prop_T_scanl", mytest prop_T_scanl),
371383 ("prop_T_scanl1", mytest prop_T_scanl1),