@@ -9,6 +9,8 @@
9{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE UndecidableInstances #-} -- XXX
10{-# LANGUAGE UndecidableInstances #-} -- XXX
13-- Module : Streamly.Streams.StreamK
15-- Module : Streamly.Streams.StreamK
14-- Copyright : (c) 2017 Harendra Kumar
16-- Copyright : (c) 2017 Harendra Kumar
@@ -227,14 +229,30 @@ uncons m =
227-- Generation
229-- Generation
228-------------------------------------------------------------------------------
230-------------------------------------------------------------------------------
232+{-# INLINE_NORMAL build #-}
233+build :: IsStream t => forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a
236+{-# INLINE_NORMAL _augment #-}
239+ => forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a -> t m a
240+_augment g xs = g cons xs
242+{-# INLINE_NORMAL _buildM #-}
244+ :: (IsStream t, MonadAsync m)
245+ => forall a. ((m a -> t m a -> t m a) -> t m a -> t m a) -> t m a
246+_buildM g = g consM nil
230{-# INLINE unfoldr #-}
248{-# INLINE unfoldr #-}
231unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
249unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
232−unfoldr step = go
250+unfoldr step b0 = build $ \cns nl ->
233− where
251+ let go s =
234− go s = mkStream $ \_ yld _ stp ->
235− case step s of
252+ case step s of
237− Just (a, b) -> yld a (go b)
253+ Just (a, b) -> a `cns` go b
239{-# INLINE unfoldrM #-}
257{-# INLINE unfoldrM #-}
240unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
258unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
@@ -405,6 +423,9 @@ foldlM' step begin = foldxM step (return begin) return
405-- Specialized folds
423-- Specialized folds
406------------------------------------------------------------------------------
424------------------------------------------------------------------------------
427+-- > runStream = foldl' (\_ _ -> ()) ()
428+-- > runStream = mapM_ (\_ -> return ())
408{-# INLINE runStream #-}
429{-# INLINE runStream #-}
409runStream :: (Monad m, IsStream t) => t m a -> m ()
430runStream :: (Monad m, IsStream t) => t m a -> m ()
410runStream = go
431runStream = go
@@ -642,6 +663,7 @@ findIndices p = go 0
643-- | Apply a monadic action to each element of the stream and discard the
664-- | Apply a monadic action to each element of the stream and discard the
644-- output of the action.
665-- output of the action.
645mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
667mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
646mapM_ f m = go m
668mapM_ f m = go m