packages feed

effectful-streamly-0.1.0.0: src/Effectful/Streamly/Stream.hs

-- |
-- Module      : Effectful.Streamly.Stream
-- Description : Streamly.Data.Stream.Prelude, for Eff
-- License     : BSD-3-Clause
--
-- A replacement for "Streamly.Data.Stream.Prelude" when the stream runs in
-- 'Eff'. The serial API and the configuration are re-exported as they
-- are. Every concurrent combinator is wrapped with
-- 'Effectful.Streamly.concurrently', so that it works whatever the
-- unlifting strategy of the consumer:
--
-- > import Effectful.Streamly.Stream (fold, parMapM)
-- > import Streamly.Data.Fold (toList)
-- >
-- > fold toList (parMapM id fetch names)
--
-- See "Effectful.Streamly" for how effects behave in worker threads.
-- The combinators that streamly deprecates are left out.
module Effectful.Streamly.Stream
  ( -- * Serial streams
    module Streamly.Data.Stream

    -- * Configuration
  , Config
  , maxThreads
  , maxBuffer
  , Rate (..)
  , rate
  , avgRate
  , minRate
  , maxRate
  , constRate
  , StopWhen (..)
  , stopWhen
  , eager
  , ordered
  , interleaved
  , useAcquire
  , clearAcquire
  , inspect

    -- * Evaluation
  , parBuffered
  , parRepeatM
  , parReplicateM
  , fromCallback

    -- * Mapping
  , parMapM
  , parSequence

    -- * Combining two streams
  , parZipWithM
  , parZipWith
  , parMergeByM
  , parMergeBy

    -- * Combining many streams
  , parList
  , parCrossApply
  , parConcat
  , parConcatMap
  , parConcatIterate

    -- * Observation
  , parTapCount

    -- * Time
  , interject
  , takeInterval
  , dropInterval
  , intervalsOf
  , sampleIntervalEnd
  , sampleIntervalStart
  , sampleBurstEnd
  , sampleBurstStart

    -- * Resources
  , after
  , bracket
  , finally
  ) where

import Effectful (Eff, IOE, type (:>))
import Effectful.Streamly (concurrently)
import Streamly.Data.Fold (Fold)
import Streamly.Data.Stream (Enumerable (..), Stream, afterIO, append, before,
                             bfsUnfoldEach, bracketIO, bracketIO', bracketIO'',
                             bracketIO3, catEithers, catLefts, catMaybes,
                             catRights, chunksOf, cmpBy, concatEffect,
                             concatFor, concatForM, concatMap, concatMapM, cons,
                             consM, cross, crossWith, delay, drop, dropWhile,
                             dropWhileM, enumerate, enumerateTo, eqBy,
                             fairConcatFor, fairConcatForM, fairConcatMap,
                             fairCross, fairUnfoldEach, filter, filterM,
                             finallyIO, finallyIO', finallyIO'', fold,
                             foldBreak, foldMany, foldr, foldrM, fromEffect,
                             fromList, fromPure, groupsOf, handle, indexed,
                             insertBy, intercalate, intercalateSuffix,
                             interleave, intersperse, intersperseM,
                             intersperseM_, isInfixOf, isPrefixOf,
                             isSubsequenceOf, iterate, iterateM, liftInner,
                             mapM, mapMaybe, mapMaybeM, mergeBy, mergeByM,
                             morphInner, nil, nilM, onException, parse,
                             parseBreak, parseBreakPos, parseMany, parsePos,
                             postscan, postscanl, repeat, repeatM, replicate,
                             replicateM, reverse, runReaderT, runStateT, scan,
                             scanMaybe, scanl, sequence, splitEndBySeq,
                             splitEndBySeq_, splitOn, splitSepBySeq_,
                             splitSepBy_, stripPrefix, take, takeWhile,
                             takeWhileM, tap, toList, trace, uncons, unfold,
                             unfoldEach, unfoldEachEndBySeq, unfoldEachSepBySeq,
                             unfoldMany, unfoldr, unfoldrM, unionBy, wordsBy,
                             zipWith, zipWithM)
import Streamly.Data.Stream.Prelude (Config, Rate (..), StopWhen (..), avgRate,
                                     clearAcquire, constRate, eager, inspect,
                                     interleaved, maxBuffer, maxRate,
                                     maxThreads, minRate, ordered, rate,
                                     stopWhen, useAcquire)
import Streamly.Data.Stream.Prelude qualified as S (after, bracket,
                                                    dropInterval, finally,
                                                    fromCallback, interject,
                                                    intervalsOf, parBuffered,
                                                    parConcat, parConcatIterate,
                                                    parConcatMap, parCrossApply,
                                                    parList, parMapM,
                                                    parMergeBy, parMergeByM,
                                                    parRepeatM, parReplicateM,
                                                    parSequence, parTapCount,
                                                    parZipWith, parZipWithM,
                                                    sampleBurstEnd,
                                                    sampleBurstStart,
                                                    sampleIntervalEnd,
                                                    sampleIntervalStart,
                                                    takeInterval)

-- Evaluation

-- | Evaluate the stream in a thread of its own, ahead of its consumer,
-- keeping the results in a buffer. The stream itself stays serial: its
-- actions do not run concurrently with each other.
parBuffered :: IOE :> es => (Config -> Config) -> Stream (Eff es) a -> Stream (Eff es) a
parBuffered cfg = concurrently . S.parBuffered cfg

-- | Run an action again and again, concurrently, as a stream of its
-- results.
parRepeatM :: IOE :> es => (Config -> Config) -> Eff es a -> Stream (Eff es) a
parRepeatM cfg = concurrently . S.parRepeatM cfg

-- | Run an action a number of times, concurrently, as a stream of its
-- results.
parReplicateM :: IOE :> es => (Config -> Config) -> Int -> Eff es a -> Stream (Eff es) a
parReplicateM cfg n = concurrently . S.parReplicateM cfg n

-- | The stream of the values given to a callback. The function receives
-- the callback and must hand it to a thread other than the one that
-- evaluates the stream, which calls it for as long as it runs.
fromCallback :: IOE :> es => ((a -> Eff es ()) -> Eff es ()) -> Stream (Eff es) a
fromCallback = concurrently . S.fromCallback

-- Mapping

-- | Map an action over the stream, several elements at a time.
parMapM :: IOE :> es => (Config -> Config) -> (a -> Eff es b) -> Stream (Eff es) a -> Stream (Eff es) b
parMapM cfg f = concurrently . S.parMapM cfg f

-- | Run the actions of a stream, several at a time.
parSequence :: IOE :> es => (Config -> Config) -> Stream (Eff es) (Eff es a) -> Stream (Eff es) a
parSequence cfg = concurrently . S.parSequence cfg

-- Combining two streams

-- | Evaluate two streams concurrently and combine their elements, pair
-- by pair, with an action.
parZipWithM
  :: IOE :> es
  => (Config -> Config)
  -> (a -> b -> Eff es c)
  -> Stream (Eff es) a
  -> Stream (Eff es) b
  -> Stream (Eff es) c
parZipWithM cfg f xs = concurrently . S.parZipWithM cfg f xs

-- | Evaluate two streams concurrently and combine their elements, pair
-- by pair.
parZipWith
  :: IOE :> es
  => (Config -> Config)
  -> (a -> b -> c)
  -> Stream (Eff es) a
  -> Stream (Eff es) b
  -> Stream (Eff es) c
parZipWith cfg f xs = concurrently . S.parZipWith cfg f xs

-- | Evaluate two streams concurrently and merge them, taking each time
-- the element the comparing action puts first.
parMergeByM
  :: IOE :> es
  => (Config -> Config)
  -> (a -> a -> Eff es Ordering)
  -> Stream (Eff es) a
  -> Stream (Eff es) a
  -> Stream (Eff es) a
parMergeByM cfg f xs = concurrently . S.parMergeByM cfg f xs

-- | Evaluate two streams concurrently and merge them, taking each time
-- the element the comparison puts first.
parMergeBy
  :: IOE :> es
  => (Config -> Config)
  -> (a -> a -> Ordering)
  -> Stream (Eff es) a
  -> Stream (Eff es) a
  -> Stream (Eff es) a
parMergeBy cfg f xs = concurrently . S.parMergeBy cfg f xs

-- Combining many streams

-- | Evaluate the streams of a list concurrently, as one stream of their
-- elements.
parList :: IOE :> es => (Config -> Config) -> [Stream (Eff es) a] -> Stream (Eff es) a
parList cfg = concurrently . S.parList cfg

-- | Apply every function of a stream to every value of another, the
-- two evaluated concurrently.
parCrossApply
  :: IOE :> es
  => (Config -> Config)
  -> Stream (Eff es) (a -> b)
  -> Stream (Eff es) a
  -> Stream (Eff es) b
parCrossApply cfg fs = concurrently . S.parCrossApply cfg fs

-- | Evaluate the streams of a stream concurrently, as one stream of
-- their elements.
parConcat :: IOE :> es => (Config -> Config) -> Stream (Eff es) (Stream (Eff es) a) -> Stream (Eff es) a
parConcat cfg = concurrently . S.parConcat cfg

-- | Map each element to a stream and evaluate these streams
-- concurrently, as one stream of their elements.
parConcatMap
  :: IOE :> es
  => (Config -> Config)
  -> (a -> Stream (Eff es) b)
  -> Stream (Eff es) a
  -> Stream (Eff es) b
parConcatMap cfg f = concurrently . S.parConcatMap cfg f

-- | Like 'parConcatMap', applied again to the elements it produces:
-- the concurrent traversal of a tree.
parConcatIterate
  :: IOE :> es
  => (Config -> Config)
  -> (a -> Stream (Eff es) a)
  -> Stream (Eff es) a
  -> Stream (Eff es) a
parConcatIterate cfg f = concurrently . S.parConcatIterate cfg f

-- Observation

-- | Count the elements that satisfy a predicate and give the stream of
-- the counts to a function, which runs in a thread of its own. The
-- elements pass unchanged.
parTapCount
  :: IOE :> es
  => (a -> Bool)
  -> (Stream (Eff es) Int -> Eff es b)
  -> Stream (Eff es) a
  -> Stream (Eff es) a
parTapCount p f = concurrently . S.parTapCount p f

-- Time

-- | Run an action every so many seconds and insert its results in the
-- stream.
interject :: IOE :> es => Eff es a -> Double -> Stream (Eff es) a -> Stream (Eff es) a
interject action seconds = concurrently . S.interject action seconds

-- | /Broken in streamly 0.11.1/: the stream ends at its first element, so
-- the result is always empty. Its definition keeps the elements while they
-- are @Nothing@ instead of while they are @Just@
-- (<https://github.com/composewell/streamly/issues/3271>). Kept for
-- compatibility.
takeInterval :: IOE :> es => Double -> Stream (Eff es) a -> Stream (Eff es) a
takeInterval seconds = concurrently . S.takeInterval seconds

-- | /Broken in streamly 0.11.1/, like 'takeInterval': nothing is dropped
-- when the stream yields at once.
dropInterval :: IOE :> es => Double -> Stream (Eff es) a -> Stream (Eff es) a
dropInterval seconds = concurrently . S.dropInterval seconds

-- | Fold the elements that arrive in each window of so many seconds;
-- the stream of the results. A fold that ends before its window does is
-- started again in the same window.
intervalsOf :: IOE :> es => Double -> Fold (Eff es) a b -> Stream (Eff es) a -> Stream (Eff es) b
intervalsOf seconds f = concurrently . S.intervalsOf seconds f

-- | The last element of each window of so many seconds. Known elsewhere
-- as throttling.
sampleIntervalEnd :: IOE :> es => Double -> Stream (Eff es) a -> Stream (Eff es) a
sampleIntervalEnd seconds = concurrently . S.sampleIntervalEnd seconds

-- | The first element of each window of so many seconds.
sampleIntervalStart :: IOE :> es => Double -> Stream (Eff es) a -> Stream (Eff es) a
sampleIntervalStart seconds = concurrently . S.sampleIntervalStart seconds

-- | The last element of each burst: a burst ends when nothing arrives
-- for so many seconds. Known elsewhere as debouncing.
sampleBurstEnd :: IOE :> es => Double -> Stream (Eff es) a -> Stream (Eff es) a
sampleBurstEnd seconds = concurrently . S.sampleBurstEnd seconds

-- | The first element of each burst: a burst ends when nothing arrives
-- for so many seconds.
sampleBurstStart :: IOE :> es => Double -> Stream (Eff es) a -> Stream (Eff es) a
sampleBurstStart seconds = concurrently . S.sampleBurstStart seconds

-- Resources

-- | Run an action when the stream stops, or when it is garbage collected
-- after a partial evaluation. The collection case runs the action in
-- another thread.
after :: IOE :> es => Eff es b -> Stream (Eff es) a -> Stream (Eff es) a
after action = concurrently . S.after action

-- | Acquire a resource, and release it when the stream stops, fails, or is
-- garbage collected after a partial evaluation. The collection case runs
-- the release in another thread.
bracket :: IOE :> es => Eff es b -> (b -> Eff es c) -> (b -> Stream (Eff es) a) -> Stream (Eff es) a
bracket acquire release = concurrently . S.bracket acquire release

-- | Run an action when the stream stops, fails, or is garbage collected
-- after a partial evaluation. The collection case runs the action in
-- another thread.
finally :: IOE :> es => Eff es b -> Stream (Eff es) a -> Stream (Eff es) a
finally action = concurrently . S.finally action