on master help sign in
Merge pull request #111 from RyanGlScott/master
0bbcc86Bryan O'Sullivan
Data/Text/Lazy.hs+26−1
@@ -125,7 +125,10 @@ module Data.Text.Lazy
125 , mapAccumR
125 , mapAccumR
126
126
127 -- ** Generation and unfolding
127 -- ** Generation and unfolding
128+ , repeat
128 , replicate
129 , replicate
130+ , cycle
131+ , iterate
129 , unfoldr
132 , unfoldr
130 , unfoldrN
133 , unfoldrN
131
134
@@ -218,7 +221,8 @@ import qualified Data.Text.Unsafe as T
218import qualified Data.Text.Internal.Lazy.Fusion as S
221import qualified Data.Text.Internal.Lazy.Fusion as S
219import Data.Text.Internal.Fusion.Types (PairS(..))
222import Data.Text.Internal.Fusion.Types (PairS(..))
220import Data.Text.Internal.Lazy.Fusion (stream, unstream)
223import 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)
222import Data.Text.Internal (firstf, safe, text)
226import Data.Text.Internal (firstf, safe, text)
223import qualified Data.Text.Internal.Functions as F
227import qualified Data.Text.Internal.Functions as F
224import Data.Text.Internal.Lazy.Search (indices)
228import Data.Text.Internal.Lazy.Search (indices)
@@ -918,6 +922,12 @@ mapAccumR f = go
918 go z Empty = (z, Empty)
922 go z Empty = (z, Empty)
919{-# INLINE mapAccumR #-}
923{-# INLINE mapAccumR #-}
920
924
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+
921-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
931-- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
922-- @t@ repeated @n@ times.
932-- @t@ repeated @n@ times.
923replicate :: Int64 -> Text -> Text
933replicate :: Int64 -> Text -> Text
@@ -929,6 +939,21 @@ replicate n t
929 | otherwise = t : rep (i+1)
939 | otherwise = t : rep (i+1)
930{-# INLINE [1] replicate #-}
940{-# INLINE [1] replicate #-}
931
941
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+
932-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
957-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
933-- value of every element. Subject to fusion.
958-- value of every element. Subject to fusion.
934replicateChar :: Int64 -> Char -> Text
959replicateChar :: 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)
440tl_mapAccumR f z = L.mapAccumR f z `eqP` (second unpackS . TL.mapAccumR f z)
440tl_mapAccumR f z = L.mapAccumR f z `eqP` (second unpackS . TL.mapAccumR f z)
441 where _types = f :: Int -> Char -> (Int,Char)
441 where _types = f :: Int -> Char -> (Int,Char)
442
442
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+
443replicate n l = concat (L.replicate n l)
447replicate n l = concat (L.replicate n l)
444
448
445s_replicate n = replicate m `eq`
449s_replicate n = replicate m `eq`
@@ -451,6 +455,14 @@ tl_replicate n = replicate m `eq`
451 (unpackS . TL.replicate (fromIntegral m) . packS)
455 (unpackS . TL.replicate (fromIntegral m) . packS)
452 where m = fromIntegral (n :: Word8)
456 where m = fromIntegral (n :: Word8)
453
457
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+
454unf :: Int -> Char -> Maybe (Char, Char)
466unf :: Int -> Char -> Maybe (Char, Char)
455unf n c | fromEnum c * 100 > n = Nothing
467unf n c | fromEnum c * 100 > n = Nothing
456 | otherwise = Just (c, succ c)
468 | otherwise = Just (c, succ c)
@@ -1094,9 +1106,12 @@ tests =
1094 ],
1106 ],
1095
1107
1096 testGroup "unfolds" [
1108 testGroup "unfolds" [
1109+ testProperty "tl_repeat" tl_repeat,
1097 testProperty "s_replicate" s_replicate,
1110 testProperty "s_replicate" s_replicate,
1098 testProperty "t_replicate" t_replicate,
1111 testProperty "t_replicate" t_replicate,
1099 testProperty "tl_replicate" tl_replicate,
1112 testProperty "tl_replicate" tl_replicate,
1113+ testProperty "tl_cycle" tl_cycle,
1114+ testProperty "tl_iterate" tl_iterate,
1100 testProperty "t_unfoldr" t_unfoldr,
1115 testProperty "t_unfoldr" t_unfoldr,
1101 testProperty "tl_unfoldr" tl_unfoldr,
1116 testProperty "tl_unfoldr" tl_unfoldr,
1102 testProperty "t_unfoldrN" t_unfoldrN,
1117 testProperty "t_unfoldrN" t_unfoldrN,