@@ -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