@@ -94,7 +94,7 @@ module Data.Text.Lazy
9494 -- * Substrings
9595
9696 -- ** Breaking strings
97− -- , take
97+ , take
9898 -- , drop
9999 -- , takeWhile
100100 -- , dropWhile
@@ -493,6 +493,27 @@ unfoldrN :: Int64 -> (a -> Maybe (Char,a)) -> a -> Text
493493unfoldrN n f s = unstream (S.unfoldrN n f s)
494494{-# INLINE unfoldrN #-}
495495
496+-- | /O(n)/ 'take' @n@, applied to a 'Text', returns the prefix of the
497+-- 'Text' of length @n@, or the 'Text' itself if @n@ is greater than
498+-- the length of the Text. Subject to fusion.
499+take :: Int64 -> Text -> Text
500+take i _ | i <= 0 = Empty
501+take i t0 = take' i t0
502+ where take' 0 _ = Empty
503+ take' _ Empty = Empty
504+ take' n (Chunk t ts)
505+ | n < len = Chunk (T.take (fromIntegral n) t) Empty
506+ | otherwise = Chunk t (take' (n - len) ts)
507+ where len = fromIntegral (T.length t)
508+{-# INLINE [1] take #-}
509+
510+{-# RULES
511+"LAZY TEXT take -> fused" [~1] forall n t.
512+ take n t = unstream (S.take n (stream t))
513+"LAZY TEXT take -> unfused" [1] forall n t.
514+ unstream (S.take n (stream t)) = take n t
515+ #-}
516+
496517-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
497518-- prefix of @t@ of length @n@, and whose second is the remainder of
498519-- the string. It is equivalent to @('take' n t, 'drop' n t)@.