on master help sign in
Implement and test lazy map, intercalate, intersperse

--HG-- extra : convert_revision : 21d9ddaefbc6b4bafaaf95d495717f72b19099cf

commitec34d1a4ffab6210ecbe6deba021fb9745e69372 authorBryan O'Sullivan <bos@serpentine.com> dateTue 3 Mar 2009, 00:38 (18 years ago) parent1876b8c
2 files changed+41−15
Data/Text/Lazy.hs+35−15 tests/Properties.hs+6
Data/Text/Lazy.hs+35−15
@@ -52,9 +52,9 @@ module Data.Text.Lazy
5252 , length
5353
5454 -- * Transformations
55− -- , map
56− -- , intercalate
57− -- , intersperse
55+ , map
56+ , intercalate
57+ , intersperse
5858 -- , transpose
5959 -- , reverse
6060
@@ -153,6 +153,7 @@ import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
153153 fromIntegral, not, return, otherwise)
154154import qualified Prelude as P
155155import Data.Int (Int64)
156+import qualified Data.List as L
156157import Data.String (IsString(..))
157158import qualified Data.Text as T
158159import qualified Data.Text.Fusion as S
@@ -296,18 +297,6 @@ null _ = False
296297 S.null (stream t) = null t
297298 #-}
298299
299−length :: Text -> Int64
300−length = foldlChunks go 0
301− where go l t = l + fromIntegral (T.length t)
302−{-# INLINE [1] length #-}
303−
304−{-# RULES
305−"LAZY TEXT length -> fused" [~1] forall t.
306− length t = S.length64 (stream t)
307−"LAZY TEXT length -> unfused" [1] forall t.
308− S.length64 (stream t) = length t
309− #-}
310−
311300-- | /O(1)/ Returns the last character of a 'Text', which must be
312301-- non-empty. Subject to array fusion.
313302last :: Text -> Char
@@ -324,6 +313,37 @@ last (Chunk t ts) = go t ts
324313 S.last (stream t) = last t
325314 #-}
326315
316+length :: Text -> Int64
317+length = foldlChunks go 0
318+ where go l t = l + fromIntegral (T.length t)
319+{-# INLINE [1] length #-}
320+
321+{-# RULES
322+"LAZY TEXT length -> fused" [~1] forall t.
323+ length t = S.length64 (stream t)
324+"LAZY TEXT length -> unfused" [1] forall t.
325+ S.length64 (stream t) = length t
326+ #-}
327+
328+-- | /O(n)/ 'map' @f @xs is the 'Text' obtained by applying @f@ to
329+-- each element of @xs@. Subject to array fusion.
330+map :: (Char -> Char) -> Text -> Text
331+map f t = unstream (S.map f (stream t))
332+{-# INLINE [1] map #-}
333+
334+-- | /O(n)/ The 'intercalate' function takes a 'Text' and a list of
335+-- 'Text's and concatenates the list after interspersing the first
336+-- argument between each element of the list.
337+intercalate :: Text -> [Text] -> Text
338+intercalate t ts = unstream (S.intercalate (stream t) (L.map stream ts))
339+{-# INLINE intercalate #-}
340+
341+-- | /O(n)/ The 'intersperse' function takes a character and places it
342+-- between the characters of a 'Text'. Subject to array fusion.
343+intersperse :: Char -> Text -> Text
344+intersperse c t = unstream (S.intersperse c (stream t))
345+{-# INLINE intersperse #-}
346+
327347-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
328348-- prefix of @t@ of length @n@, and whose second is the remainder of
329349-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
tests/Properties.hs+6
@@ -141,8 +141,11 @@ prop_S_length64 = length `eqP` (fromIntegral . S.length64)
141141prop_T_length = length `eqP` T.length
142142prop_TL_length = length `eqP` (fromIntegral . TL.length)
143143prop_T_map f = map f `eqP` (unpackT . T.map f)
144+prop_TL_map f = map f `eqP` (unpackT . TL.map f)
144145prop_T_intercalate c = L.intercalate c `eq` (unpackT . T.intercalate (T.pack c) . map T.pack)
146+prop_TL_intercalate c = L.intercalate c `eq` (unpackT . TL.intercalate (TL.pack c) . map TL.pack)
145147prop_T_intersperse c = L.intersperse c `eqP` (unpackT . T.intersperse c)
148+prop_TL_intersperse c = L.intersperse c `eqP` (unpackT . TL.intersperse c)
146149prop_T_transpose = L.transpose `eq` (map unpackT . T.transpose . map T.pack)
147150prop_T_reverse = L.reverse `eqP` (unpackT . T.reverse)
148151prop_T_reverse_short n = L.reverse `eqP` (unpackT . S.reverse . shorten n . S.stream)
@@ -326,8 +329,11 @@ tests = [
326329 ("prop_TL_length", mytest prop_TL_length),
327330
328331 ("prop_T_map", mytest prop_T_map),
332+ ("prop_TL_map", mytest prop_TL_map),
329333 ("prop_T_intercalate", mytest prop_T_intercalate),
334+ ("prop_TL_intercalate", mytest prop_TL_intercalate),
330335 ("prop_T_intersperse", mytest prop_T_intersperse),
336+ ("prop_TL_intersperse", mytest prop_TL_intersperse),
331337 ("prop_T_transpose", mytest prop_T_transpose),
332338 ("prop_T_reverse", mytest prop_T_reverse),
333339 ("prop_T_reverse_short", mytest prop_T_reverse_short),