on master help sign in
Implement replicate

--HG-- extra : convert_revision : 903d412b43ef7aa1439df1d6bd51e0824de97cdb

commitfa5c09f43a5f52d7f17616d0b59b8e126e9d2c80 authorBryan O'Sullivan <bos@serpentine.com> dateTue 27 Jan 2009, 07:25 (18 years ago) parent245819f
2 files changed+17−4
Data/Text.hs+5 Data/Text/Fusion.hs+12−4
Data/Text.hs+5
@@ -72,6 +72,7 @@ module Data.Text
7272 , minimum
7373
7474 -- * Construction
75+ , replicate
7576 , unfoldr
7677 , unfoldrN
7778
@@ -425,6 +426,10 @@ minimum t = S.minimum (stream t)
425426-- -----------------------------------------------------------------------------
426427-- ** Generating and unfolding 'Text's
427428
429+replicate :: Int -> Char -> Text
430+replicate n c = unstream (S.replicate n c)
431+{-# INLINE replicate #-}
432+
428433-- | /O(n)/, where @n@ is the length of the result. The 'unfoldr'
429434-- function is analogous to the List 'L.unfoldr'. 'unfoldr' builds a
430435-- 'Text' from a seed value. The function takes the element and
Data/Text/Fusion.hs+12−4
@@ -60,6 +60,7 @@ module Data.Text.Fusion
6060 , minimum
6161
6262 -- * Construction
63+ , replicate
6364 , unfoldr
6465 , unfoldrN
6566
@@ -84,10 +85,9 @@ module Data.Text.Fusion
8485 , zipWith
8586 ) where
8687
87−import Prelude hiding
88− (map, tail, head, foldr, filter, concat, last, init, null, length, foldl,
89− foldl1, foldr1, concatMap, any, all, maximum, minimum, take, drop,
90− takeWhile, dropWhile, elem, zipWith, reverse)
88+import Prelude (Bool(..), Char, Either(..), Eq(..), Maybe(..), Monad(..),
89+ Num(..), Ord(..), String, ($), (++), (.), (&&), error,
90+ fromIntegral, fst, otherwise, snd)
9191import Data.Char (ord)
9292import Control.Monad (liftM2)
9393import Control.Monad.ST (runST, ST)
@@ -543,6 +543,14 @@ minimum (Stream next0 s0 _len) = loop0_minimum s0
543543-- -----------------------------------------------------------------------------
544544-- ** Generating and unfolding streams
545545
546+replicate :: Int -> Char -> Stream Char
547+replicate n c = Stream next 0 n
548+ where
549+ {-# INLINE next #-}
550+ next i | i >= n = Done
551+ | otherwise = Yield c (i + 1)
552+{-# INLINE [0] replicate #-}
553+
546554-- | /O(n)/, where @n@ is the length of the result. The unfoldr function
547555-- is analogous to the List 'unfoldr'. unfoldr builds a stream
548556-- from a seed value. The function takes the element and returns