on master help sign in
Implement inits and tails
a85f987Bryan O'Sullivan
Data/Text.hs+13−2
@@ -100,8 +100,8 @@ module Data.Text
100 -- , break
100 -- , break
101 -- , group
101 -- , group
102 -- , groupBy
102 -- , groupBy
103− -- , inits
103+ , inits
104− -- , tails
104+ , tails
105
105
106 -- ** Breaking into many substrings
106 -- ** Breaking into many substrings
107 -- , split
107 -- , split
@@ -557,6 +557,17 @@ takeWhile p t = unstream (S.takeWhile p (stream t))
557dropWhile :: (Char -> Bool) -> Text -> Text
557dropWhile :: (Char -> Bool) -> Text -> Text
558dropWhile p t = unstream (S.dropWhile p (stream t))
558dropWhile p t = unstream (S.dropWhile p (stream t))
559
559
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+
560-- ----------------------------------------------------------------------------
571-- ----------------------------------------------------------------------------
561-- * Searching
572-- * Searching
562
573