Skip to content
Browse files

Add a lazier implementation of intersperse on lists

--HG--
extra : convert_revision : ad41a033cecf5bec60c2e9dd13c895d66e9dee42
  • Loading branch information...
1 parent f3dd63d commit 00c70eb27eca47bb5394e5b44ea063522d973c64 @bos committed Sep 2, 2010
Showing with 32 additions and 2 deletions.
  1. +2 −1 Data/Text.hs
  2. +2 −1 Data/Text/Lazy.hs
  3. +27 −0 Data/Text/Util.hs
  4. +1 −0 text.cabal
View
3 Data/Text.hs
@@ -198,6 +198,7 @@ import qualified Prelude as P
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)
@@ -473,7 +474,7 @@ map f t = unstream (S.map f (stream t))
-- '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
View
3 Data/Text/Lazy.hs
@@ -201,6 +201,7 @@ import Data.Text.Fusion.Internal (PairS(..))
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
@@ -447,7 +448,7 @@ map f t = unstream (S.map f (stream t))
-- '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
View
27 Data/Text/Util.hs
@@ -0,0 +1,27 @@
+{-# 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 #-}
View
1 text.cabal
@@ -88,6 +88,7 @@ library
Data.Text.Unsafe
Data.Text.UnsafeChar
Data.Text.UnsafeShift
+ Data.Text.Util
build-depends:
base < 5,

0 comments on commit 00c70eb

Please sign in to comment.
Something went wrong with that request. Please try again.