on master help sign in
Add tests, clarify documentation
d334976RyanGlScott
Data/Text/Lazy.hs+4−4
@@ -939,8 +939,8 @@ replicate n t
939 | otherwise = t : rep (i+1)
939 | otherwise = t : rep (i+1)
940{-# INLINE [1] replicate #-}
940{-# INLINE [1] replicate #-}
941
941
942−-- | 'cycle' ties a finite 'Text' into a circular one, or equivalently,
942+-- | 'cycle' ties a finite, non-empty 'Text' into a circular one, or
943−-- the infinite repetition of the original 'Text'.
943+-- equivalently, the infinite repetition of the original 'Text'.
944cycle :: Text -> Text
944cycle :: Text -> Text
945cycle Empty = emptyError "cycle"
945cycle Empty = emptyError "cycle"
946cycle t = let t' = foldrChunks Chunk t' t
946cycle t = let t' = foldrChunks Chunk t' t
@@ -951,8 +951,8 @@ cycle t = let t' = foldrChunks Chunk t' t
951--
951--
952-- > iterate f x == [x, f x, f (f x), ...]
952-- > iterate f x == [x, f x, f (f x), ...]
953iterate :: (Char -> Char) -> Char -> Text
953iterate :: (Char -> Char) -> Char -> Text
954−iterate f = unfoldr $ \x -> case f x of
954+iterate f c = let t c' = Chunk (T.singleton c') (t (f c'))
955− !x' -> Just (x', x')
955+ in t c
956
956
957-- | /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
958-- value of every element. Subject to fusion.
958-- value of every element. Subject to fusion.
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,