on master help sign in
Add tests, clarify documentation
commitd334976c0d593aa179a58e44fcc641c87488801b authorRyanGlScott <ryan.gl.scott@ku.edu> dateSat 31 Jan 2015, 13:55 (12 years ago) parentf53f4a3
2 files changed+19−4
Data/Text/Lazy.hs+4−4 tests/Tests/Properties.hs+15
Data/Text/Lazy.hs+4−4
@@ -939,8 +939,8 @@ replicate n t
939939 | otherwise = t : rep (i+1)
940940{-# INLINE [1] replicate #-}
941941
942−-- | 'cycle' ties a finite 'Text' into a circular one, or equivalently,
943−-- the infinite repetition of the original 'Text'.
942+-- | 'cycle' ties a finite, non-empty 'Text' into a circular one, or
943+-- equivalently, the infinite repetition of the original 'Text'.
944944cycle :: Text -> Text
945945cycle Empty = emptyError "cycle"
946946cycle t = let t' = foldrChunks Chunk t' t
@@ -951,8 +951,8 @@ cycle t = let t' = foldrChunks Chunk t' t
951951--
952952-- > iterate f x == [x, f x, f (f x), ...]
953953iterate :: (Char -> Char) -> Char -> Text
954−iterate f = unfoldr $ \x -> case f x of
955− !x' -> Just (x', x')
954+iterate f c = let t c' = Chunk (T.singleton c') (t (f c'))
955+ in t c
956956
957957-- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the
958958-- 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)
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,