on master help sign in
Speed up intersperse?
c67c26bBryan O'Sullivan
Data/Text/Fusion/Common.hs+12−10
@@ -296,22 +296,24 @@ map f (Stream next0 s0 len) = Stream next s0 len
296 map f (map g s) = map (\x -> f (g x)) s
296 map f (map g s) = map (\x -> f (g x)) s
297 #-}
297 #-}
298
298
299+data I s = I1 !s
300+ | I2 !s {-# UNPACK #-} !Char
301+ | I3 !s
302+
299-- | /O(n)/ Take a character and place it between each of the
303-- | /O(n)/ Take a character and place it between each of the
300-- characters of a 'Stream Char'.
304-- characters of a 'Stream Char'.
301intersperse :: Char -> Stream Char -> Stream Char
305intersperse :: Char -> Stream Char -> Stream Char
302−intersperse c (Stream next0 s0 len) = Stream next (s0 :*: N :*: S1) len
306+intersperse c (Stream next0 s0 len) = Stream next (I1 s0) len
303 where
307 where
304− {-# INLINE next #-}
305− next (s :*: N :*: S1) = case next0 s of
308+ next (I1 s) = case next0 s of
306 Done -> Done
309 Done -> Done
307− Skip s' -> Skip (s' :*: N :*: S1)
310+ Skip s' -> Skip (I1 s')
308− Yield x s' -> Skip (s' :*: J x :*: S1)
311+ Yield x s' -> Skip (I2 s' x)
309− next (s :*: J x :*: S1) = Yield x (s :*: N :*: S2)
312+ next (I2 s x) = Yield x (I3 s)
310− next (s :*: N :*: S2) = case next0 s of
313+ next (I3 s) = case next0 s of
311 Done -> Done
314 Done -> Done
312− Skip s' -> Skip (s' :*: N :*: S2)
315+ Skip s' -> Skip (I3 s')
313− Yield x s' -> Yield c (s' :*: J x :*: S1)
316+ Yield x s' -> Yield c (I2 s' x)
314− next _ = internalError "intersperse"
315{-# INLINE [0] intersperse #-}
317{-# INLINE [0] intersperse #-}
316
318
317-- ----------------------------------------------------------------------------
319-- ----------------------------------------------------------------------------