@@ -121,6 +121,55 @@ length :: Stream Char -> Int
121length = S.lengthI
121length = S.lengthI
122{-# INLINE[0] length #-}
122{-# INLINE[0] length #-}
124+-- | /O(n)/ Returns the number of characters in a text.
125+length64 :: Stream Char -> Int64
126+length64 (Stream next s0 _len) = loop_length 0 s0
128+ loop_length z s = case next s of
130+ Skip s' -> loop_length z s'
131+ Yield _ s' -> let !z' = z + 1
132+ in loop_length z' s'
133+{-# INLINE[0] length64 #-}
135+-- ----------------------------------------------------------------------------
136+-- * Stream transformations
138+-- | /O(n)/ 'map' @f @xs is the Stream Char obtained by applying @f@ to each element of
140+map :: (Char -> Char) -> Stream Char -> Stream Char
141+map f (Stream next0 s0 len) = Stream next s0 len
144+ next !s = case next0 s of
147+ Yield x s' -> Yield (f x) s'
148+{-# INLINE [0] map #-}
151+ RULES "STREAM map/map fusion" forall f g s.
152+ map f (map g s) = map (\x -> f (g x)) s
155+-- | /O(n)/ Take a character and place it between each of the
156+-- characters of a 'Stream Char'.
157+intersperse :: Char -> Stream Char -> Stream Char
158+intersperse c (Stream next0 s0 len) = Stream next (s0 :!: Nothing :!: S1) len
161+ next (s :!: Nothing :!: S1) = case next0 s of
163+ Skip s' -> Skip (s' :!: Nothing :!: S1)
164+ Yield x s' -> Skip (s' :!: Just x :!: S1)
165+ next (s :!: Just x :!: S1) = Yield x (s :!: Nothing :!: S2)
166+ next (s :!: Nothing :!: S2) = case next0 s of
168+ Skip s' -> Skip (s' :!: Nothing :!: S2)
169+ Yield x s' -> Yield c (s' :!: Just x :!: S1)
170+ next _ = internalError "intersperse"
171+{-# INLINE [0] intersperse #-}
124-- | /O(n)/ Reverse the characters of a string.
173-- | /O(n)/ Reverse the characters of a string.
125reverse :: Stream Char -> Text
174reverse :: Stream Char -> Text
126reverse (Stream next s len0)
175reverse (Stream next s len0)