on master help sign in
Merge
commitf3f3681b52263e557f8aa920a8226d9df95ed2d4 authorBryan O'Sullivan <bos@serpentine.com> dateTue 17 Feb 2015, 22:03 (12 years ago) parentf74427c0c2ff73
2 files changed+41−1
Data/Text/Lazy.hs+26−1 tests/Tests/Properties.hs+15
Data/Text/Lazy.hs+26−1
@@ -125,7 +125,10 @@ module Data.Text.Lazy
125125 , mapAccumR
126126
127127 -- ** Generation and unfolding
128+ , repeat
128129 , replicate
130+ , cycle
131+ , iterate
129132 , unfoldr
130133 , unfoldrN
131134
@@ -218,7 +221,8 @@ import qualified Data.Text.Unsafe as T
218221import qualified Data.Text.Internal.Lazy.Fusion as S
219222import Data.Text.Internal.Fusion.Types (PairS(..))
220223import Data.Text.Internal.Lazy.Fusion (stream, unstream)
221−import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks, foldrChunks)
224+import Data.Text.Internal.Lazy (Text(..), chunk, empty, foldlChunks,
225+ foldrChunks, smallChunkSize)
222226import Data.Text.Internal (firstf, safe, text)
223227import qualified Data.Text.Internal.Functions as F
224228import Data.Text.Internal.Lazy.Search (indices)
@@ -918,6 +922,12 @@ mapAccumR f = go
918922 go z Empty = (z, Empty)
919923{-# INLINE mapAccumR #-}
920924
925+-- | @'repeat' x@ is an infinite 'Text', with @x@ the value of every
926+-- element.
927+repeat :: Char -> Text
928+repeat c = let t = Chunk (T.replicate smallChunkSize (T.singleton c)) t
929+ in t
930+
921931-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
922932-- @t@ repeated @n@ times.
923933replicate :: Int64 -> Text -> Text
@@ -929,6 +939,21 @@ replicate n t
929939 | otherwise = t : rep (i+1)
930940{-# INLINE [1] replicate #-}
931941
942+-- | 'cycle' ties a finite, non-empty 'Text' into a circular one, or
943+-- equivalently, the infinite repetition of the original 'Text'.
944+cycle :: Text -> Text
945+cycle Empty = emptyError "cycle"
946+cycle t = let t' = foldrChunks Chunk t' t
947+ in t'
948+
949+-- | @'iterate' f x@ returns an infinite 'Text' of repeated applications
950+-- of @f@ to @x@:
951+--
952+-- > iterate f x == [x, f x, f (f x), ...]
953+iterate :: (Char -> Char) -> Char -> Text
954+iterate f c = let t c' = Chunk (T.singleton c') (t (f c'))
955+ in t c
956+
932957-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
933958-- value of every element. Subject to fusion.
934959replicateChar :: Int64 -> Char -> Text
tests/Tests/Properties.hs+15
@@ -440,6 +440,10 @@ t_mapAccumR f z = L.mapAccumR f z `eqP` (second unpackS . T.mapAccumR f z)
440440tl_mapAccumR f z = L.mapAccumR f z `eqP` (second unpackS . TL.mapAccumR f z)
441441 where _types = f :: Int -> Char -> (Int,Char)
442442
443+tl_repeat n = (L.take m . L.repeat) `eq`
444+ (unpackS . TL.take (fromIntegral m) . TL.repeat)
445+ where m = fromIntegral (n :: Word8)
446+
443447replicate n l = concat (L.replicate n l)
444448
445449s_replicate n = replicate m `eq`
@@ -451,6 +455,14 @@ tl_replicate n = replicate m `eq`
451455 (unpackS . TL.replicate (fromIntegral m) . packS)
452456 where m = fromIntegral (n :: Word8)
453457
458+tl_cycle n = (L.take m . L.cycle) `eq`
459+ (unpackS . TL.take (fromIntegral m) . TL.cycle . packS)
460+ where m = fromIntegral (n :: Word8)
461+
462+tl_iterate f n = (L.take m . L.iterate f) `eq`
463+ (unpackS . TL.take (fromIntegral m) . TL.iterate f)
464+ where m = fromIntegral (n :: Word8)
465+
454466unf :: Int -> Char -> Maybe (Char, Char)
455467unf n c | fromEnum c * 100 > n = Nothing
456468 | otherwise = Just (c, succ c)
@@ -1094,9 +1106,12 @@ tests =
10941106 ],
10951107
10961108 testGroup "unfolds" [
1109+ testProperty "tl_repeat" tl_repeat,
10971110 testProperty "s_replicate" s_replicate,
10981111 testProperty "t_replicate" t_replicate,
10991112 testProperty "tl_replicate" tl_replicate,
1113+ testProperty "tl_cycle" tl_cycle,
1114+ testProperty "tl_iterate" tl_iterate,
11001115 testProperty "t_unfoldr" t_unfoldr,
11011116 testProperty "tl_unfoldr" tl_unfoldr,
11021117 testProperty "t_unfoldrN" t_unfoldrN,