@@ -9,6 +9,8 @@
99{-# LANGUAGE ScopedTypeVariables #-}
1010{-# LANGUAGE UndecidableInstances #-} -- XXX
1111
12+#include "inline.hs"
13+
1214-- |
1315-- Module : Streamly.Streams.StreamK
1416-- Copyright : (c) 2017 Harendra Kumar
@@ -227,14 +229,30 @@ uncons m =
227229-- Generation
228230-------------------------------------------------------------------------------
229231
232+{-# INLINE_NORMAL build #-}
233+build :: IsStream t => forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a
234+build g = g cons nil
235+
236+{-# INLINE_NORMAL _augment #-}
237+_augment
238+ :: IsStream t
239+ => forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a -> t m a
240+_augment g xs = g cons xs
241+
242+{-# INLINE_NORMAL _buildM #-}
243+_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
247+
230248{-# INLINE unfoldr #-}
231249unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
232−unfoldr step = go
233− where
234− go s = mkStream $ \_ yld _ stp ->
235− case step s of
236− Nothing -> stp
237− Just (a, b) -> yld a (go b)
250+unfoldr step b0 = build $ \cns nl ->
251+ let go s =
252+ case step s of
253+ Just (a, b) -> a `cns` go b
254+ Nothing -> nl
255+ in go b0
238256
239257{-# INLINE unfoldrM #-}
240258unfoldrM :: (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
405423-- Specialized folds
406424------------------------------------------------------------------------------
407425
426+-- |
427+-- > runStream = foldl' (\_ _ -> ()) ()
428+-- > runStream = mapM_ (\_ -> return ())
408429{-# INLINE runStream #-}
409430runStream :: (Monad m, IsStream t) => t m a -> m ()
410431runStream = go
@@ -642,6 +663,7 @@ findIndices p = go 0
642663
643664-- | Apply a monadic action to each element of the stream and discard the
644665-- output of the action.
666+{-# INLINE mapM_ #-}
645667mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
646668mapM_ f m = go m
647669 where