@@ -53,6 +53,8 @@ module Asyncly.Streams
5353 , fromSVar
5454
5555 -- * Elimination
56+ , cons
57+ , nil
5658 , streamFold
5759 , runStreaming
5860 , toSVar
@@ -77,6 +79,7 @@ module Asyncly.Streams
7779 , (<|)
7880
7981 -- * Fold Utilities
82+ -- $foldutils
8083 , foldWith
8184 , foldMapWith
8285 , forEachWith
@@ -740,6 +743,12 @@ zippingAsync x = x
740743-- Constructing a stream
741744------------------------------------------------------------------------------
742745
746+cons :: (Streaming t) => a -> t m a -> t m a
747+cons a r = fromStream $ Stream $ \_ _ yld -> yld a (Just (toStream r))
748+
749+nil :: Streaming t => t m a
750+nil = fromStream $ Stream $ \_ stp _ -> stp
751+
743752-- | Build a stream from its church encoding. The function passed maps
744753-- directly to the underlying representation of the stream type. The second
745754-- parameter to the function is the "yield" function yielding a value and the
@@ -865,21 +874,33 @@ m1 <| m2 = fromStream $ parLeft (toStream m1) (toStream m2)
865874-- Fold Utilities
866875------------------------------------------------------------------------------
867876
868−-- | Fold a 'Foldable' container using the given function.
877+-- $foldutils
878+-- These utilities are designed to pass the first argument as one of the sum
879+-- style composition operators (i.e. '<>', '<=>', '<|', '<|>') to conveniently
880+-- fold a container using any style of stream composition.
881+
882+-- | Like the 'Prelude' 'fold' but allows you to specify a binary sum style
883+-- stream composition operator to fold a container of streams.
884+--
885+-- @foldWith (<>) $ map return [1..3]@
869886{-# INLINABLE foldWith #-}
870−foldWith :: (Monoid b, Foldable t) => (a -> b -> b) -> t a -> b
871−foldWith f = foldr f mempty
887+foldWith :: (Streaming t, Foldable f)
888+ => (t m a -> t m a -> t m a) -> f (t m a) -> t m a
889+foldWith f = foldr f nil
872890
873−-- | Fold a 'Foldable' container using a function that is a composition of the
874−-- two arguments.
891+-- | Like 'foldMap' but allows you to specify a binary sum style composition
892+-- operator to fold a container of streams. Maps a monadic streaming action on
893+-- the container before folding it.
894+--
895+-- @foldMapWith (<>) return [1..3]@
875896{-# INLINABLE foldMapWith #-}
876−foldMapWith :: (Monoid b, Foldable t) =>
877− (b1 -> b -> b) -> (a -> b1) -> t a -> b
878−foldMapWith f g = foldr (f . g) mempty
897+foldMapWith :: (Streaming t, Foldable f)
898+ => (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
899+foldMapWith f g = foldr (f . g) nil
879900
880−-- | Fold a 'Foldable' container using a function that is a composition of the
881−-- first and the third argument.
901+-- | Like 'foldMapWith' but with the last two arguments reversed i.e. the
902+-- monadic streaming function is the last argument.
882903{-# INLINABLE forEachWith #-}
883−forEachWith :: (Monoid b, Foldable t) =>
884− (b1 -> b -> b) -> t a -> (a -> b1) -> b
885−forEachWith f xs g = foldr (f . g) mempty xs
904+forEachWith :: (Streaming t, Foldable f)
905+ => (t m b -> t m b -> t m b) -> f a -> (a -> t m b) -> t m b
906+forEachWith f xs g = foldr (f . g) nil xs