on master help sign in
Implement and test lazy inits and tails

--HG-- extra : convert_revision : 4fe002d1bd0d7f0d61d8b8dc8b187ca402961775

commitcb8194dc665aac51084a25dd4b1e7b39ca498953 authorBryan O'Sullivan <bos@serpentine.com> dateWed 4 Mar 2009, 06:05 (18 years ago) parent815eeee
2 files changed+22−2
Data/Text/Lazy.hs+18−2 tests/Properties.hs+4
Data/Text/Lazy.hs+18−2
@@ -103,8 +103,8 @@ module Data.Text.Lazy
103103 , break
104104 , group
105105 , groupBy
106− -- , inits
107− -- , tails
106+ , inits
107+ , tails
108108
109109 -- ** Breaking into many substrings
110110 -- , split
@@ -630,6 +630,22 @@ groupBy eq (Chunk t ts) = cons x ys : groupBy eq zs
630630 x = T.unsafeHead t
631631 xs = chunk (T.unsafeTail t) ts
632632
633+-- | /O(n)/ Return all initial segments of the given 'Text',
634+-- shortest first.
635+inits :: Text -> [Text]
636+inits = (Empty :) . inits'
637+ where inits' Empty = []
638+ inits' (Chunk t ts) = L.map (\t' -> Chunk t' Empty) (L.tail (T.inits t))
639+ ++ L.map (Chunk t) (inits' ts)
640+
641+-- | /O(n)/ Return all final segments of the given 'Text', longest
642+-- first.
643+tails :: Text -> [Text]
644+tails Empty = Empty : []
645+tails ts@(Chunk t ts')
646+ | T.length t == 1 = ts : tails ts'
647+ | otherwise = ts : tails (Chunk (T.unsafeTail t) ts')
648+
633649revChunks :: [T.Text] -> Text
634650revChunks = L.foldl' (flip chunk) Empty
635651
tests/Properties.hs+4
@@ -238,7 +238,9 @@ prop_TL_group = L.group `eqP` (map unpackT . TL.group)
238238prop_T_groupBy p = L.groupBy p `eqP` (map unpackT . T.groupBy p)
239239prop_TL_groupBy p = L.groupBy p `eqP` (map unpackT . TL.groupBy p)
240240prop_T_inits = L.inits `eqP` (map unpackT . T.inits)
241+prop_TL_inits = L.inits `eqP` (map unpackT . TL.inits)
241242prop_T_tails = L.tails `eqP` (map unpackT . T.tails)
243+prop_TL_tails = L.tails `eqP` (map unpackT . TL.tails)
242244
243245prop_T_split_i c = id `eq` (T.intercalate (T.singleton c) . T.split c)
244246
@@ -440,7 +442,9 @@ tests = [
440442 ("prop_T_groupBy", mytest prop_T_groupBy),
441443 ("prop_TL_groupBy", mytest prop_TL_groupBy),
442444 ("prop_T_inits", mytest prop_T_inits),
445+ ("prop_TL_inits", mytest prop_TL_inits),
443446 ("prop_T_tails", mytest prop_T_tails),
447+ ("prop_TL_tails", mytest prop_TL_tails),
444448
445449 ("prop_T_split_i", mytest prop_T_split_i),
446450 ("prop_T_splitWith", mytest prop_T_splitWith),