@@ -90,13 +90,11 @@ import qualified Streamly.Internal.Data.Stream.StreamK as K
90-- the stream.
90-- the stream.
91data Step s a = Yield a s | Skip s | Stop
91data Step s a = Yield a s | Skip s | Stop
94instance Functor (Step s) where
93instance Functor (Step s) where
95 {-# INLINE fmap #-}
94 {-# INLINE fmap #-}
96 fmap f (Yield x s) = Yield (f x) s
95 fmap f (Yield x s) = Yield (f x) s
97 fmap _ (Skip s) = Skip s
96 fmap _ (Skip s) = Skip s
98 fmap _ Stop = Stop
97 fmap _ Stop = Stop
101-- gst = global state
99-- gst = global state
102-- | A stream consists of a step function that generates the next step given a
100-- | A stream consists of a step function that generates the next step given a
@@ -176,9 +174,12 @@ mapM f (Stream step state) = Stream step' state
176map :: Monad m => (a -> b) -> Stream m a -> Stream m b
174map :: Monad m => (a -> b) -> Stream m a -> Stream m b
177map f = mapM (return . f)
175map f = mapM (return . f)
179−instance Monad m => Functor (Stream m) where
177+instance Functor m => Functor (Stream m) where
180 {-# INLINE fmap #-}
178 {-# INLINE fmap #-}
181− fmap = map
179+ fmap f (Stream step state) = Stream step' state
181+ {-# INLINE_LATE step' #-}
182+ step' gst st = fmap (fmap f) (step (adaptState gst) st)
183------------------------------------------------------------------------------
184------------------------------------------------------------------------------
184-- concatMap
185-- concatMap
@@ -231,23 +232,61 @@ concatMap f = concatMapM (return . f)
232-- | Create a singleton 'Stream' from a pure value.
233-- | Create a singleton 'Stream' from a pure value.
233{-# INLINE_NORMAL yield #-}
234{-# INLINE_NORMAL yield #-}
234−yield :: Monad m => a -> Stream m a
235+yield :: Applicative m => a -> Stream m a
235−yield x = Stream (\_ s -> return $ step undefined s) True
236+yield x = Stream (\_ s -> pure $ step undefined s) True
237 {-# INLINE_LATE step #-}
238 {-# INLINE_LATE step #-}
238 step _ True = Yield x False
239 step _ True = Yield x False
239 step _ False = Stop
240 step _ False = Stop
242+{-# INLINE_NORMAL concatAp #-}
243+concatAp :: Functor f => Stream f (a -> b) -> Stream f a -> Stream f b
244+concatAp (Stream stepa statea) (Stream stepb stateb) = Stream step' (Left statea)
246+ {-# INLINE_LATE step' #-}
247+ step' gst (Left st) = fmap
249+ Yield f s -> Skip (Right (f, s, stateb))
250+ Skip s -> Skip (Left s)
252+ (stepa (adaptState gst) st)
253+ step' gst (Right (f, os, st)) = fmap
255+ Yield a s -> Yield (f a) (Right (f, os, s))
256+ Skip s -> Skip (Right (f,os, s))
257+ Stop -> Skip (Left os))
258+ (stepb (adaptState gst) st)
260+{-# INLINE_NORMAL apSequence #-}
261+apSequence :: Functor f => Stream f a -> Stream f b -> Stream f b
262+apSequence (Stream stepa statea) (Stream stepb stateb) = Stream step (Left statea)
264+ {-# INLINE_LATE step #-}
265+ step gst (Left st) =
269+ Yield _ s -> Skip (Right (s, stateb))
270+ Skip s -> Skip (Left s)
272+ (stepa (adaptState gst) st)
273+ step gst (Right (ostate, st)) =
277+ Yield b s -> Yield b (Right (ostate, s))
278+ Skip s -> Skip (Right (ostate, s))
279+ Stop -> Skip (Left ostate))
241−instance Monad m => Applicative (Stream m) where
282+instance Applicative f => Applicative (Stream f) where
242 {-# INLINE pure #-}
283 {-# INLINE pure #-}
243 pure = yield
284 pure = yield
244 {-# INLINE (<*>) #-}
285 {-# INLINE (<*>) #-}
245− m1 <*> m2 =
286+ (<*>) = concatAp
246− -- XXX concatMapU is faster but it would require us to merge
287+ {-# INLINE (*>) #-}
247− -- Unfold/Types.hs into this file so that we can use UF.singleton here.
288+ (*>) = apSequence
248− -- let f x1 = concatMapU (UF.singleton (pure . x1)) m2
289+
249− let f x1 = concatMap (\x2 -> pure (x1 x2)) m2
252-- NOTE: even though concatMap for StreamD is 4x faster compared to StreamK,
291-- NOTE: even though concatMap for StreamD is 4x faster compared to StreamK,
253-- the monad instance does not seem to be significantly faster.
292-- the monad instance does not seem to be significantly faster.
@@ -256,6 +295,8 @@ instance Monad m => Monad (Stream m) where
256 return = pure
295 return = pure
257 {-# INLINE (>>=) #-}
296 {-# INLINE (>>=) #-}
258 (>>=) = flip concatMap
297 (>>=) = flip concatMap
260instance MonadTrans Stream where
301instance MonadTrans Stream where
261 lift = yieldM
302 lift = yieldM
@@ -425,12 +466,12 @@ foldl' fstep = foldlM' (\b a -> return (fstep b a))
426-- | Convert a list of pure values to a 'Stream'
467-- | Convert a list of pure values to a 'Stream'
427{-# INLINE_LATE fromList #-}
468{-# INLINE_LATE fromList #-}
428−fromList :: Monad m => [a] -> Stream m a
469+fromList :: Applicative m => [a] -> Stream m a
429fromList = Stream step
470fromList = Stream step
431 {-# INLINE_LATE step #-}
472 {-# INLINE_LATE step #-}
432− step _ (x:xs) = return $ Yield x xs
473+ step _ (x:xs) = pure $ Yield x xs
433− step _ [] = return Stop
474+ step _ [] = pure Stop
435------------------------------------------------------------------------------
476------------------------------------------------------------------------------
436-- Comparisons
477-- Comparisons