Commits

Bryan O'Sullivan committed 76d3053

Implement inits and tails

Comments (0)

Files changed (1)

     -- , break
     -- , group
     -- , groupBy
-    -- , inits
-    -- , tails
+    , inits
+    , tails
 
     -- ** Breaking into many substrings
     -- , split
 dropWhile :: (Char -> Bool) -> Text -> Text
 dropWhile p t = unstream (S.dropWhile p (stream t))
 
+-- | /O(n)/ Return all initial segments of the given 'Text', shortest
+-- first.
+inits :: Text -> [Text]
+inits (Text arr off len) = [Text arr off l | l <- [0..len]]
+
+-- | /O(n)/ Return all final segments of the given 'Text', longest
+-- first.
+tails :: Text -> [Text]
+tails p | null p    = [empty]
+        | otherwise = p : tails (tail p)
+
 -- ----------------------------------------------------------------------------
 -- * Searching