on master help sign in
Implement inits and tails

--HG-- extra : convert_revision : a12fbf366e16cd59ccadcab117a20b150ebe0358

commita85f987edd1d2a9c3287f1149daf32e1f8c0a1c6 authorBryan O'Sullivan <bos@serpentine.com> dateWed 28 Jan 2009, 04:45 (18 years ago) parent802c8a8
1 file changed+13−2
Data/Text.hs+13−2
Data/Text.hs+13−2
@@ -100,8 +100,8 @@ module Data.Text
100100 -- , break
101101 -- , group
102102 -- , groupBy
103− -- , inits
104− -- , tails
103+ , inits
104+ , tails
105105
106106 -- ** Breaking into many substrings
107107 -- , split
@@ -557,6 +557,17 @@ takeWhile p t = unstream (S.takeWhile p (stream t))
557557dropWhile :: (Char -> Bool) -> Text -> Text
558558dropWhile p t = unstream (S.dropWhile p (stream t))
559559
560+-- | /O(n)/ Return all initial segments of the given 'Text', shortest
561+-- first.
562+inits :: Text -> [Text]
563+inits (Text arr off len) = [Text arr off l | l <- [0..len]]
564+
565+-- | /O(n)/ Return all final segments of the given 'Text', longest
566+-- first.
567+tails :: Text -> [Text]
568+tails p | null p = [empty]
569+ | otherwise = p : tails (tail p)
570+
560571-- ----------------------------------------------------------------------------
561572-- * Searching
562573