Commits

Bryan O'Sullivan  committed 889128b

Add a lazier implementation of intersperse on lists

  • Participants
  • Parent commits dd0f8a9

Comments (0)

Files changed (4)

File Data/Text.hs

 import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
                          unsafeHead, unsafeTail)
 import Data.Text.UnsafeChar (unsafeChr)
+import qualified Data.Text.Util as U
 import qualified Data.Text.Encoding.Utf16 as U16
 import Data.Text.Search (indices)
 
 -- 'Text's and concatenates the list after interspersing the first
 -- argument between each element of the list.
 intercalate :: Text -> [Text] -> Text
-intercalate t = concat . (L.intersperse t)
+intercalate t = concat . (U.intersperse t)
 {-# INLINE intercalate #-}
 
 -- | /O(n)/ The 'intersperse' function takes a character and places it

File Data/Text/Lazy.hs

 import Data.Text.Lazy.Fusion (stream, unstream)
 import Data.Text.Lazy.Internal (Text(..), chunk, empty, foldlChunks, foldrChunks)
 import Data.Text.Internal (textP)
+import qualified Data.Text.Util as U
 import Data.Text.Lazy.Search (indices)
 
 instance Eq Text where
 -- 'Text's and concatenates the list after interspersing the first
 -- argument between each element of the list.
 intercalate :: Text -> [Text] -> Text
-intercalate t = concat . (L.intersperse t)
+intercalate t = concat . (U.intersperse t)
 {-# INLINE intercalate #-}
 
 -- | /O(n)/ The 'intersperse' function takes a character and places it

File Data/Text/Util.hs

+{-# LANGUAGE CPP, DeriveDataTypeable #-}
+
+-- |
+-- Module      : Data.Text.Util
+-- Copyright   : 2010 Bryan O'Sullivan
+--
+-- License     : BSD-style
+-- Maintainer  : bos@serpentine.com
+-- Stability   : experimental
+-- Portability : GHC
+--
+-- Useful functions.
+
+module Data.Text.Util
+    (
+      intersperse
+    ) where
+
+-- | A lazier version of Data.List.intersperse.  The other version
+-- causes space leaks!
+intersperse :: a -> [a] -> [a]
+intersperse _   []     = []
+intersperse sep (x:xs) = x : go xs
+  where
+    go []     = []
+    go (y:ys) = sep : y: go ys
+{-# INLINE intersperse #-}
     Data.Text.Unsafe
     Data.Text.UnsafeChar
     Data.Text.UnsafeShift
+    Data.Text.Util
 
   build-depends:
     base       < 5,