{-# LANGUAGE CPP #-}
-- |
-- Module      : Streamly.Internal.Data.Stream.Transform
-- Copyright   : (c) 2018 Composewell Technologies
--               (c) Roman Leshchinskiy 2008-2010
-- License     : BSD-3-Clause
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC

-- A few functions in this module have been adapted from the vector package
-- (c) Roman Leshchinskiy. See the notes in specific combinators.

module Streamly.Internal.Data.Stream.Transform
    (
    -- * Mapping
    -- | Stateless one-to-one maps.
      sequence

    -- * Mapping Effects
    , tap
    , tapOffsetEvery
    , trace
    , trace_

    -- * Folding
    , foldrS
    , foldlS

    -- * Composable Scans
    , postscanl
    , scanl
    , scanlMany
    , scanr
    , pipe

    -- * Splitting
    , splitSepBy_

    -- * Ad-hoc Scans
    -- | Left scans. Stateful, mostly one-to-one maps.
    , scanlM'
    , scanlMAfter'
    , scanl'
    , scanlM
    , scanlBy
    , scanl1M'
    , scanl1'
    , scanl1M
    , scanl1

    , prescanl'
    , prescanlM'

    , postscanlBy
    , postscanlM
    , postscanl'
    , postscanlM'
    , postscanlMAfter'

    , postscanlx'
    , postscanlMx'
    , scanlMx'
    , scanlx'

    -- * Filtering
    -- delete is for once like insert, filter is for many like intersperse.

    -- | Produce a subset of the stream.
    , with
    , postscanlMaybe
    , filter -- retainBy
    , filterM
    , deleteBy -- deleteOnceBy/deleteFirstBy?
    , uniqBy
    , uniq
    , prune
    , repeated

    -- * Sampling
    -- | Value agnostic filtering.
    , sampleFromThen
    -- keepEvery/filterEvery -- sampling
    -- deleteEvery/dropEvery/removeEvery -- dual of intersperseEvery
    -- deintersperse - drop infixed elements

    -- * Trimming
    -- | Produce a subset of the stream trimmed at ends.
    , initNonEmpty
    , tailNonEmpty
    , drop
    , dropWhile
    , dropWhileM

    -- * Trimming from end
    -- | RingArray array based or buffering operations.
    --
    , takeWhileLast
    , takeWhileAround
    , dropLast
    , dropWhileLast
    , dropWhileAround

    -- * Inserting Elements
    -- insert is for once like delete, intersperse is for many like filter
    -- | Produce a superset of the stream. Value agnostic insertion.
    , intersperse
    , intersperseM
    , intersperseEveryM
    , intersperseEndByM
    , intersperseEndByEveryM

    -- Value aware insertion.
    , insertBy -- insertCmpBy
    -- insertBeforeBy
    -- insertAfterBy
    -- intersperseBeforeBy
    -- intersperseAfterBy

    -- * Inserting Side Effects
    , intersperseM_
    , intersperseEndByM_
    , intersperseBeginByM_

    , delay
    , delayPre
    , delayPost

    -- * Reordering
    -- | Produce strictly the same set but reordered.
    , reverse
    , reverseUnbox
    , reassembleBy

    -- * Position Indexing
    , indexed
    , indexedR

    -- * Time Indexing
    , timestampWith
    , timestamped
    , timeIndexWith
    , timeIndexed

    -- * Searching
    , findIndices
    , elemIndices

    -- * Rolling map
    -- | Map using the previous element.
    , rollingMap
    , rollingMapM
    , rollingMap2

    -- * Maybe Streams
    , mapMaybe
    , mapMaybeM
    , catMaybes

    -- * Either Streams
    , catLefts
    , catRights
    , catEithers

    -- * Deprecated
    , postscan
    , scan
    , scanMany
    , scanMaybe
    , intersperseMSuffix
    , intersperseMSuffixWith
    , intersperseMSuffix_
    , intersperseMPrefix_
    , strideFromThen
    , splitOn
    )
where

#include "deprecation.h"
#include "inline.hs"

import Control.Concurrent (threadDelay)
import Control.Monad (void)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Either (fromLeft, isLeft, isRight, fromRight)
import Data.Functor ((<&>))
import Data.Maybe (fromJust, isJust)
import Fusion.Plugin.Types (Fuse(..))

import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Pipe.Type (Pipe(..))
import Streamly.Internal.Data.Scanl.Type (Scanl(..))
import Streamly.Internal.Data.Scanr (Scanr(..))
import Streamly.Internal.Data.SVar.Type (adaptState)
import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64)
import Streamly.Internal.Data.Unbox (Unbox)
import Streamly.Internal.System.IO (defaultChunkSize)

-- import qualified Data.List as List
import qualified Streamly.Internal.Data.Array.Type as A
import qualified Streamly.Internal.Data.Fold.Type as FL
import qualified Streamly.Internal.Data.Pipe.Type as Pipe
import qualified Streamly.Internal.Data.StreamK.Type as K

import Prelude hiding
       ( drop, dropWhile, filter, map, mapM, reverse
       , scanl, scanl1, scanr, sequence, take, takeWhile, zipWith)

import Streamly.Internal.Data.Stream.Generate
    (absTimesWith, relTimesWith)
import Streamly.Internal.Data.Stream.Type

#include "DocTestDataStream.hs"

------------------------------------------------------------------------------
-- Piping
------------------------------------------------------------------------------

{-# ANN type PipeState Fuse #-}
data PipeState st sc ps = PipeConsume st sc | PipeProduce st ps

-- | Use a 'Pipe' to transform a stream.
--
{-# INLINE_NORMAL pipe #-}
pipe :: Monad m => Pipe m a b -> Stream m a -> Stream m b
pipe :: forall (m :: * -> *) a b.
Monad m =>
Pipe m a b -> Stream m a -> Stream m b
pipe (Pipe cs -> a -> m (Step cs ps b)
consume ps -> m (Step cs ps b)
produce cs
initial) (Stream State StreamK m a -> s -> m (Step s a)
stream_step s
state) =
    (State StreamK m b
 -> PipeState s cs ps -> m (Step (PipeState s cs ps) b))
-> PipeState s cs ps -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b
-> PipeState s cs ps -> m (Step (PipeState s cs ps) b)
forall {m :: * -> *} {a}.
State StreamK m a
-> PipeState s cs ps -> m (Step (PipeState s cs ps) b)
step (s -> cs -> PipeState s cs ps
forall st sc ps. st -> sc -> PipeState st sc ps
PipeConsume s
state cs
initial)

    where

    {-# INLINE goConsume #-}
    goConsume :: st -> cs -> a -> m (Step (PipeState st cs ps) b)
goConsume st
st cs
cs a
x = do
        Step cs ps b
res <- cs -> a -> m (Step cs ps b)
consume cs
cs a
x
        Step (PipeState st cs ps) b -> m (Step (PipeState st cs ps) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (PipeState st cs ps) b -> m (Step (PipeState st cs ps) b))
-> Step (PipeState st cs ps) b -> m (Step (PipeState st cs ps) b)
forall a b. (a -> b) -> a -> b
$ case Step cs ps b
res of
                Pipe.YieldC cs
s b
b -> b -> PipeState st cs ps -> Step (PipeState st cs ps) b
forall s a. a -> s -> Step s a
Yield b
b (st -> cs -> PipeState st cs ps
forall st sc ps. st -> sc -> PipeState st sc ps
PipeConsume st
st cs
s)
                Pipe.SkipC cs
s -> PipeState st cs ps -> Step (PipeState st cs ps) b
forall s a. s -> Step s a
Skip (st -> cs -> PipeState st cs ps
forall st sc ps. st -> sc -> PipeState st sc ps
PipeConsume st
st cs
s)
                Step cs ps b
Pipe.Stop -> Step (PipeState st cs ps) b
forall s a. Step s a
Stop
                Pipe.YieldP ps
ps b
b -> b -> PipeState st cs ps -> Step (PipeState st cs ps) b
forall s a. a -> s -> Step s a
Yield b
b (st -> ps -> PipeState st cs ps
forall st sc ps. st -> ps -> PipeState st sc ps
PipeProduce st
st ps
ps)
                Pipe.SkipP ps
ps -> PipeState st cs ps -> Step (PipeState st cs ps) b
forall s a. s -> Step s a
Skip (st -> ps -> PipeState st cs ps
forall st sc ps. st -> ps -> PipeState st sc ps
PipeProduce st
st ps
ps)

    {-# INLINE_LATE step #-}
    step :: State StreamK m a
-> PipeState s cs ps -> m (Step (PipeState s cs ps) b)
step State StreamK m a
gst (PipeConsume s
st cs
cs) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
stream_step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> s -> cs -> a -> m (Step (PipeState s cs ps) b)
forall {st}. st -> cs -> a -> m (Step (PipeState st cs ps) b)
goConsume s
s cs
cs a
x
            Skip s
s -> Step (PipeState s cs ps) b -> m (Step (PipeState s cs ps) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (PipeState s cs ps) b -> m (Step (PipeState s cs ps) b))
-> Step (PipeState s cs ps) b -> m (Step (PipeState s cs ps) b)
forall a b. (a -> b) -> a -> b
$ PipeState s cs ps -> Step (PipeState s cs ps) b
forall s a. s -> Step s a
Skip (s -> cs -> PipeState s cs ps
forall st sc ps. st -> sc -> PipeState st sc ps
PipeConsume s
s cs
cs)
            Step s a
Stop -> Step (PipeState s cs ps) b -> m (Step (PipeState s cs ps) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (PipeState s cs ps) b
forall s a. Step s a
Stop
    step State StreamK m a
_ (PipeProduce s
st ps
ps) = do
        Step cs ps b
r <- ps -> m (Step cs ps b)
produce ps
ps
        Step (PipeState s cs ps) b -> m (Step (PipeState s cs ps) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (PipeState s cs ps) b -> m (Step (PipeState s cs ps) b))
-> Step (PipeState s cs ps) b -> m (Step (PipeState s cs ps) b)
forall a b. (a -> b) -> a -> b
$ case Step cs ps b
r of
                Pipe.YieldC cs
cs b
b -> b -> PipeState s cs ps -> Step (PipeState s cs ps) b
forall s a. a -> s -> Step s a
Yield b
b (s -> cs -> PipeState s cs ps
forall st sc ps. st -> sc -> PipeState st sc ps
PipeConsume s
st cs
cs)
                Pipe.SkipC cs
cs -> PipeState s cs ps -> Step (PipeState s cs ps) b
forall s a. s -> Step s a
Skip (s -> cs -> PipeState s cs ps
forall st sc ps. st -> sc -> PipeState st sc ps
PipeConsume s
st cs
cs)
                Step cs ps b
Pipe.Stop -> Step (PipeState s cs ps) b
forall s a. Step s a
Stop
                Pipe.YieldP ps
ps1 b
b -> b -> PipeState s cs ps -> Step (PipeState s cs ps) b
forall s a. a -> s -> Step s a
Yield b
b (s -> ps -> PipeState s cs ps
forall st sc ps. st -> ps -> PipeState st sc ps
PipeProduce s
st ps
ps1)
                Pipe.SkipP ps
ps1 -> PipeState s cs ps -> Step (PipeState s cs ps) b
forall s a. s -> Step s a
Skip (s -> ps -> PipeState s cs ps
forall st sc ps. st -> ps -> PipeState st sc ps
PipeProduce s
st ps
ps1)

{-# ANN type RunScanState Fuse #-}
data RunScanState st sc ps = ScanConsume st sc

-- | Use a lazy right 'Scanr' to transform a stream.
--
-- The following example extracts the input stream up to a point where the
-- running average of elements is no more than 10:
--
-- >>> import Data.Maybe (fromJust)
-- >>> let avg = Scanr.teeWith (/) Scanr.sum (fmap fromIntegral Scanr.length)
-- >>> s = Stream.enumerateFromTo 1.0 100.0
-- >>> :{
--  Stream.fold Fold.toList
--   $ fmap fst
--   $ Stream.takeWhile (\(_,x) -> x <= 10)
--   $ Stream.scanr (Scanr.tee Scanr.identity avg) s
-- :}
-- [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0]
--
{-# INLINE_NORMAL scanr #-}
scanr :: Monad m => Scanr m a b -> Stream m a -> Stream m b
scanr :: forall (m :: * -> *) a b.
Monad m =>
Scanr m a b -> Stream m a -> Stream m b
scanr (Scanr s -> a -> m (Step s b)
consume s
initial) (Stream State StreamK m a -> s -> m (Step s a)
stream_step s
state) =
    (State StreamK m b
 -> RunScanState s s Any -> m (Step (RunScanState s s Any) b))
-> RunScanState s s Any -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b
-> RunScanState s s Any -> m (Step (RunScanState s s Any) b)
forall {m :: * -> *} {a} {ps} {ps}.
State StreamK m a
-> RunScanState s s ps -> m (Step (RunScanState s s ps) b)
step (s -> s -> RunScanState s s Any
forall st sc ps. st -> sc -> RunScanState st sc ps
ScanConsume s
state s
initial)

    where

    {-# INLINE_LATE step #-}
    step :: State StreamK m a
-> RunScanState s s ps -> m (Step (RunScanState s s ps) b)
step State StreamK m a
gst (ScanConsume s
st s
cs) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
stream_step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                Step s b
res <- s -> a -> m (Step s b)
consume s
cs a
x
                Step (RunScanState s s ps) b -> m (Step (RunScanState s s ps) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
                    (Step (RunScanState s s ps) b -> m (Step (RunScanState s s ps) b))
-> Step (RunScanState s s ps) b -> m (Step (RunScanState s s ps) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
                        Yield b
b s
cs1 -> b -> RunScanState s s ps -> Step (RunScanState s s ps) b
forall s a. a -> s -> Step s a
Yield b
b (s -> s -> RunScanState s s ps
forall st sc ps. st -> sc -> RunScanState st sc ps
ScanConsume s
s s
cs1)
                        Skip s
cs1 -> RunScanState s s ps -> Step (RunScanState s s ps) b
forall s a. s -> Step s a
Skip (s -> s -> RunScanState s s ps
forall st sc ps. st -> sc -> RunScanState st sc ps
ScanConsume s
s s
cs1)
                        Step s b
Stop -> Step (RunScanState s s ps) b
forall s a. Step s a
Stop
            Skip s
s -> Step (RunScanState s s ps) b -> m (Step (RunScanState s s ps) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (RunScanState s s ps) b -> m (Step (RunScanState s s ps) b))
-> Step (RunScanState s s ps) b -> m (Step (RunScanState s s ps) b)
forall a b. (a -> b) -> a -> b
$ RunScanState s s ps -> Step (RunScanState s s ps) b
forall s a. s -> Step s a
Skip (s -> s -> RunScanState s s ps
forall st sc ps. st -> sc -> RunScanState st sc ps
ScanConsume s
s s
cs)
            Step s a
Stop -> Step (RunScanState s s ps) b -> m (Step (RunScanState s s ps) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (RunScanState s s ps) b
forall s a. Step s a
Stop

------------------------------------------------------------------------------
-- Transformation Folds
------------------------------------------------------------------------------

-- Note, this is going to have horrible performance, because of the nature of
-- the stream type (i.e. direct stream vs CPS). Its only for reference, it is
-- likely be practically unusable.
{-# INLINE_NORMAL foldlS #-}
foldlS :: Monad m
    => (Stream m b -> a -> Stream m b) -> Stream m b -> Stream m a -> Stream m b
foldlS :: forall (m :: * -> *) b a.
Monad m =>
(Stream m b -> a -> Stream m b)
-> Stream m b -> Stream m a -> Stream m b
foldlS Stream m b -> a -> Stream m b
fstep Stream m b
begin (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m b
 -> Either (s, Stream m b) (Stream m b)
 -> m (Step (Either (s, Stream m b) (Stream m b)) b))
-> Either (s, Stream m b) (Stream m b) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b
-> Either (s, Stream m b) (Stream m b)
-> m (Step (Either (s, Stream m b) (Stream m b)) b)
forall {m :: * -> *} {a}.
State StreamK m a
-> Either (s, Stream m b) (Stream m b)
-> m (Step (Either (s, Stream m b) (Stream m b)) b)
step' ((s, Stream m b) -> Either (s, Stream m b) (Stream m b)
forall a b. a -> Either a b
Left (s
state, Stream m b
begin))
  where
    step' :: State StreamK m a
-> Either (s, Stream m b) (Stream m b)
-> m (Step (Either (s, Stream m b) (Stream m b)) b)
step' State StreamK m a
gst (Left (s
st, Stream m b
acc)) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        Step (Either (s, Stream m b) (Stream m b)) b
-> m (Step (Either (s, Stream m b) (Stream m b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either (s, Stream m b) (Stream m b)) b
 -> m (Step (Either (s, Stream m b) (Stream m b)) b))
-> Step (Either (s, Stream m b) (Stream m b)) b
-> m (Step (Either (s, Stream m b) (Stream m b)) b)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
            Yield a
x s
s -> Either (s, Stream m b) (Stream m b)
-> Step (Either (s, Stream m b) (Stream m b)) b
forall s a. s -> Step s a
Skip ((s, Stream m b) -> Either (s, Stream m b) (Stream m b)
forall a b. a -> Either a b
Left (s
s, Stream m b -> a -> Stream m b
fstep Stream m b
acc a
x))
            Skip s
s -> Either (s, Stream m b) (Stream m b)
-> Step (Either (s, Stream m b) (Stream m b)) b
forall s a. s -> Step s a
Skip ((s, Stream m b) -> Either (s, Stream m b) (Stream m b)
forall a b. a -> Either a b
Left (s
s, Stream m b
acc))
            Step s a
Stop   -> Either (s, Stream m b) (Stream m b)
-> Step (Either (s, Stream m b) (Stream m b)) b
forall s a. s -> Step s a
Skip (Stream m b -> Either (s, Stream m b) (Stream m b)
forall a b. b -> Either a b
Right Stream m b
acc)

    step' State StreamK m a
gst (Right (Stream State StreamK m b -> s -> m (Step s b)
stp s
stt)) = do
        Step s b
r <- State StreamK m b -> s -> m (Step s b)
stp (State StreamK m a -> State StreamK m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
stt
        Step (Either (s, Stream m b) (Stream m b)) b
-> m (Step (Either (s, Stream m b) (Stream m b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either (s, Stream m b) (Stream m b)) b
 -> m (Step (Either (s, Stream m b) (Stream m b)) b))
-> Step (Either (s, Stream m b) (Stream m b)) b
-> m (Step (Either (s, Stream m b) (Stream m b)) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
            Yield b
x s
s -> b
-> Either (s, Stream m b) (Stream m b)
-> Step (Either (s, Stream m b) (Stream m b)) b
forall s a. a -> s -> Step s a
Yield b
x (Stream m b -> Either (s, Stream m b) (Stream m b)
forall a b. b -> Either a b
Right ((State StreamK m b -> s -> m (Step s b)) -> s -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> s -> m (Step s b)
stp s
s))
            Skip s
s -> Either (s, Stream m b) (Stream m b)
-> Step (Either (s, Stream m b) (Stream m b)) b
forall s a. s -> Step s a
Skip (Stream m b -> Either (s, Stream m b) (Stream m b)
forall a b. b -> Either a b
Right ((State StreamK m b -> s -> m (Step s b)) -> s -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> s -> m (Step s b)
stp s
s))
            Step s b
Stop   -> Step (Either (s, Stream m b) (Stream m b)) b
forall s a. Step s a
Stop

------------------------------------------------------------------------------
-- Transformation by Mapping
------------------------------------------------------------------------------

-- |
-- >>> sequence = Stream.mapM id
--
-- Replace the elements of a stream of monadic actions with the outputs of
-- those actions.
--
-- >>> s = Stream.fromList [putStr "a", putStr "b", putStrLn "c"]
-- >>> Stream.fold Fold.drain $ Stream.sequence s
-- abc
--
{-# INLINE_NORMAL sequence #-}
sequence :: Monad m => Stream m (m a) -> Stream m a
sequence :: forall (m :: * -> *) a. Monad m => Stream m (m a) -> Stream m a
sequence (Stream State StreamK m (m a) -> s -> m (Step s (m a))
step s
state) = (State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
forall {m :: * -> *} {a}. State StreamK m a -> s -> m (Step s a)
step' s
state
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> s -> m (Step s a)
step' State StreamK m a
gst s
st = do
         Step s (m a)
r <- State StreamK m (m a) -> s -> m (Step s (m a))
step (State StreamK m a -> State StreamK m (m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
         case Step s (m a)
r of
             Yield m a
x s
s -> m a
x m a -> (a -> m (Step s a)) -> m (Step s a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> s -> Step s a
forall s a. a -> s -> Step s a
Yield a
a s
s)
             Skip s
s    -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s
             Step s (m a)
Stop      -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s a
forall s a. Step s a
Stop

------------------------------------------------------------------------------
-- Mapping side effects
------------------------------------------------------------------------------

data TapState fs st a
    = TapInit | Tapping !fs st | TapDone st

-- XXX Multiple yield points

-- | Tap the data flowing through a stream into a 'Fold'. For example, you may
-- add a tap to log the contents flowing through the stream. The fold is used
-- only for effects, its result is discarded.
--
-- @
--                   Fold m a b
--                       |
-- -----stream m a ---------------stream m a-----
--
-- @
--
-- >>> s = Stream.enumerateFromTo 1 2
-- >>> Stream.fold Fold.drain $ Stream.tap (Fold.drainMapM print) s
-- 1
-- 2
--
-- Compare with 'trace'.
--
{-# INLINE tap #-}
tap :: Monad m => Fold m a b -> Stream m a -> Stream m a
tap :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m a
tap (Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
_ s -> m b
final) (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a
 -> TapState s s Any -> m (Step (TapState s s Any) a))
-> TapState s s Any -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a
-> TapState s s Any -> m (Step (TapState s s Any) a)
forall {a} {a}.
State StreamK m a -> TapState s s a -> m (Step (TapState s s a) a)
step' TapState s s Any
forall fs st a. TapState fs st a
TapInit

    where

    step' :: State StreamK m a -> TapState s s a -> m (Step (TapState s s a) a)
step' State StreamK m a
_ TapState s s a
TapInit = do
        Step s b
res <- m (Step s b)
initial
        Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (TapState s s a) a -> m (Step (TapState s s a) a))
-> Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a b. (a -> b) -> a -> b
$ TapState s s a -> Step (TapState s s a) a
forall s a. s -> Step s a
Skip
            (TapState s s a -> Step (TapState s s a) a)
-> TapState s s a -> Step (TapState s s a) a
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
                  FL.Partial s
s -> s -> s -> TapState s s a
forall fs st a. fs -> st -> TapState fs st a
Tapping s
s s
state
                  FL.Done b
_ -> s -> TapState s s a
forall fs st a. st -> TapState fs st a
TapDone s
state
    step' State StreamK m a
gst (Tapping s
acc s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> do
                Step s b
res <- s -> a -> m (Step s b)
fstep s
acc a
x
                Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
                    (Step (TapState s s a) a -> m (Step (TapState s s a) a))
-> Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a b. (a -> b) -> a -> b
$ a -> TapState s s a -> Step (TapState s s a) a
forall s a. a -> s -> Step s a
Yield a
x
                    (TapState s s a -> Step (TapState s s a) a)
-> TapState s s a -> Step (TapState s s a) a
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
                          FL.Partial s
fs -> s -> s -> TapState s s a
forall fs st a. fs -> st -> TapState fs st a
Tapping s
fs s
s
                          FL.Done b
_ -> s -> TapState s s a
forall fs st a. st -> TapState fs st a
TapDone s
s
            Skip s
s -> Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (TapState s s a) a -> m (Step (TapState s s a) a))
-> Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a b. (a -> b) -> a -> b
$ TapState s s a -> Step (TapState s s a) a
forall s a. s -> Step s a
Skip (s -> s -> TapState s s a
forall fs st a. fs -> st -> TapState fs st a
Tapping s
acc s
s)
            Step s a
Stop -> do
                m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m b -> m ()) -> m b -> m ()
forall a b. (a -> b) -> a -> b
$ s -> m b
final s
acc
                Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (TapState s s a) a
forall s a. Step s a
Stop
    step' State StreamK m a
gst (TapDone s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (TapState s s a) a -> m (Step (TapState s s a) a))
-> Step (TapState s s a) a -> m (Step (TapState s s a) a)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
                  Yield a
x s
s -> a -> TapState s s a -> Step (TapState s s a) a
forall s a. a -> s -> Step s a
Yield a
x (s -> TapState s s a
forall fs st a. st -> TapState fs st a
TapDone s
s)
                  Skip s
s -> TapState s s a -> Step (TapState s s a) a
forall s a. s -> Step s a
Skip (s -> TapState s s a
forall fs st a. st -> TapState fs st a
TapDone s
s)
                  Step s a
Stop -> Step (TapState s s a) a
forall s a. Step s a
Stop

data TapOffState fs s a
    = TapOffInit
    | TapOffTapping !fs s Int
    | TapOffDone s

-- XXX Multiple yield points
{-# INLINE_NORMAL tapOffsetEvery #-}
tapOffsetEvery :: Monad m
    => Int -> Int -> Fold m a b -> Stream m a -> Stream m a
tapOffsetEvery :: forall (m :: * -> *) a b.
Monad m =>
Int -> Int -> Fold m a b -> Stream m a -> Stream m a
tapOffsetEvery Int
offset Int
n (Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
_ s -> m b
final) (Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
    (State StreamK m a
 -> TapOffState s s Any -> m (Step (TapOffState s s Any) a))
-> TapOffState s s Any -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a
-> TapOffState s s Any -> m (Step (TapOffState s s Any) a)
forall {a} {a}.
State StreamK m a
-> TapOffState s s a -> m (Step (TapOffState s s a) a)
step' TapOffState s s Any
forall fs s a. TapOffState fs s a
TapOffInit

    where

    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a
-> TapOffState s s a -> m (Step (TapOffState s s a) a)
step' State StreamK m a
_ TapOffState s s a
TapOffInit = do
        Step s b
res <- m (Step s b)
initial
        Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a))
-> Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a b. (a -> b) -> a -> b
$ TapOffState s s a -> Step (TapOffState s s a) a
forall s a. s -> Step s a
Skip
            (TapOffState s s a -> Step (TapOffState s s a) a)
-> TapOffState s s a -> Step (TapOffState s s a) a
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
                  FL.Partial s
s -> s -> s -> Int -> TapOffState s s a
forall fs s a. fs -> s -> Int -> TapOffState fs s a
TapOffTapping s
s s
state (Int
offset Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
n)
                  FL.Done b
_ -> s -> TapOffState s s a
forall fs s a. s -> TapOffState fs s a
TapOffDone s
state
    step' State StreamK m a
gst (TapOffTapping s
acc s
st Int
count) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> do
                TapOffState s s a
next <-
                    if Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
                    then do
                        Step s b
res <- s -> a -> m (Step s b)
fstep s
acc a
x
                        TapOffState s s a -> m (TapOffState s s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
                            (TapOffState s s a -> m (TapOffState s s a))
-> TapOffState s s a -> m (TapOffState s s a)
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
                                  FL.Partial s
sres ->
                                    s -> s -> Int -> TapOffState s s a
forall fs s a. fs -> s -> Int -> TapOffState fs s a
TapOffTapping s
sres s
s (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
                                  FL.Done b
_ -> s -> TapOffState s s a
forall fs s a. s -> TapOffState fs s a
TapOffDone s
s
                    else TapOffState s s a -> m (TapOffState s s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (TapOffState s s a -> m (TapOffState s s a))
-> TapOffState s s a -> m (TapOffState s s a)
forall a b. (a -> b) -> a -> b
$ s -> s -> Int -> TapOffState s s a
forall fs s a. fs -> s -> Int -> TapOffState fs s a
TapOffTapping s
acc s
s (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
                Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a))
-> Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a b. (a -> b) -> a -> b
$ a -> TapOffState s s a -> Step (TapOffState s s a) a
forall s a. a -> s -> Step s a
Yield a
x TapOffState s s a
next
            Skip s
s -> Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a))
-> Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a b. (a -> b) -> a -> b
$ TapOffState s s a -> Step (TapOffState s s a) a
forall s a. s -> Step s a
Skip (s -> s -> Int -> TapOffState s s a
forall fs s a. fs -> s -> Int -> TapOffState fs s a
TapOffTapping s
acc s
s Int
count)
            Step s a
Stop -> do
                m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m b -> m ()) -> m b -> m ()
forall a b. (a -> b) -> a -> b
$ s -> m b
final s
acc
                Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (TapOffState s s a) a
forall s a. Step s a
Stop
    step' State StreamK m a
gst (TapOffDone s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a))
-> Step (TapOffState s s a) a -> m (Step (TapOffState s s a) a)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
                  Yield a
x s
s -> a -> TapOffState s s a -> Step (TapOffState s s a) a
forall s a. a -> s -> Step s a
Yield a
x (s -> TapOffState s s a
forall fs s a. s -> TapOffState fs s a
TapOffDone s
s)
                  Skip s
s -> TapOffState s s a -> Step (TapOffState s s a) a
forall s a. s -> Step s a
Skip (s -> TapOffState s s a
forall fs s a. s -> TapOffState fs s a
TapOffDone s
s)
                  Step s a
Stop -> Step (TapOffState s s a) a
forall s a. Step s a
Stop

-- | Apply a monadic function to each element flowing through the stream and
-- discard the results.
--
-- >>> s = Stream.enumerateFromTo 1 2
-- >>> Stream.fold Fold.drain $ Stream.trace print s
-- 1
-- 2
--
-- Compare with 'tap'.
--
{-# INLINE trace #-}
trace :: Monad m => (a -> m b) -> Stream m a -> Stream m a
trace :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m a
trace a -> m b
f = (a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM (\a
x -> m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (a -> m b
f a
x) m () -> m a -> m a
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x)

-- | Perform a side effect before yielding each element of the stream and
-- discard the results.
--
-- >>> s = Stream.enumerateFromTo 1 2
-- >>> Stream.fold Fold.drain $ Stream.trace_ (print "got here") s
-- "got here"
-- "got here"
--
-- Same as 'intersperseMPrefix_' but always serial.
--
-- See also: 'trace'
--
-- /Pre-release/
{-# INLINE trace_ #-}
trace_ :: Monad m => m b -> Stream m a -> Stream m a
trace_ :: forall (m :: * -> *) b a.
Monad m =>
m b -> Stream m a -> Stream m a
trace_ m b
eff = (a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM (\a
x -> m b
eff m b -> m a -> m a
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x)

------------------------------------------------------------------------------
-- Scanning with a Fold
------------------------------------------------------------------------------

data ScanState s f = ScanInit s | ScanDo s !f | ScanDone

-- NOTE: Lazy postscans can be useful e.g. to use a lazy postscan on "latest".
-- We can keep the initial state undefined in lazy postscans which do not use
-- it at all. Otherwise we have to wrap the accumulator in a Maybe type.
-- Unfortunately, we cannot define lazy scans because the Partial constructor
-- itself is strict.

-- | Postscan a stream using the given fold. A postscan omits the initial
-- (default) value of the accumulator and includes the final value.
--
-- >>> Stream.toList $ Stream.postscanl Scanl.latest (Stream.fromList [])
-- []
--
-- Compare with 'scan' which includes the initial value as well:
--
-- >>> Stream.toList $ Stream.scanl Scanl.latest (Stream.fromList [])
-- [Nothing]
--
-- The following example extracts the input stream up to a point where the
-- running average of elements is no more than 10:
--
-- >>> import Data.Maybe (fromJust)
-- >>> let avg = Scanl.teeWith (/) Scanl.sum (fmap fromIntegral Scanl.length)
-- >>> s = Stream.enumerateFromTo 1.0 100.0
-- >>> :{
--  Stream.fold Fold.toList
--   $ fmap (fromJust . fst)
--   $ Stream.takeWhile (\(_,x) -> x <= 10)
--   $ Stream.postscanl (Scanl.tee Scanl.latest avg) s
-- :}
-- [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0]
--
{-# INLINE_NORMAL postscanl #-}
postscanl :: Monad m => Scanl m a b -> Stream m a -> Stream m b
postscanl :: forall (m :: * -> *) a b.
Monad m =>
Scanl m a b -> Stream m a -> Stream m b
postscanl (Scanl s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
extract s -> m b
final) (Stream State StreamK m a -> s -> m (Step s a)
sstep s
state) =
    (State StreamK m b -> ScanState s s -> m (Step (ScanState s s) b))
-> ScanState s s -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> ScanState s s -> m (Step (ScanState s s) b)
forall {m :: * -> *} {a}.
State StreamK m a -> ScanState s s -> m (Step (ScanState s s) b)
step (s -> ScanState s s
forall s f. s -> ScanState s f
ScanInit s
state)

    where

    {-# INLINE_LATE step #-}
    step :: State StreamK m a -> ScanState s s -> m (Step (ScanState s s) b)
step State StreamK m a
_ (ScanInit s
st) = do
        Step s b
res <- m (Step s b)
initial
        Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (ScanState s s) b -> m (Step (ScanState s s) b))
-> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
res of
                  FL.Partial s
fs -> ScanState s s -> Step (ScanState s s) b
forall s a. s -> Step s a
Skip (ScanState s s -> Step (ScanState s s) b)
-> ScanState s s -> Step (ScanState s s) b
forall a b. (a -> b) -> a -> b
$ s -> s -> ScanState s s
forall s f. s -> f -> ScanState s f
ScanDo s
st s
fs
                  FL.Done b
b -> b -> ScanState s s -> Step (ScanState s s) b
forall s a. a -> s -> Step s a
Yield b
b ScanState s s
forall s f. ScanState s f
ScanDone
    step State StreamK m a
gst (ScanDo s
st s
fs) = do
        Step s a
res <- State StreamK m a -> s -> m (Step s a)
sstep (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
res of
            Yield a
x s
s -> do
                Step s b
r <- s -> a -> m (Step s b)
fstep s
fs a
x
                case Step s b
r of
                    FL.Partial s
fs1 -> do
                        !b
b <- s -> m b
extract s
fs1
                        Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ScanState s s) b -> m (Step (ScanState s s) b))
-> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a b. (a -> b) -> a -> b
$ b -> ScanState s s -> Step (ScanState s s) b
forall s a. a -> s -> Step s a
Yield b
b (ScanState s s -> Step (ScanState s s) b)
-> ScanState s s -> Step (ScanState s s) b
forall a b. (a -> b) -> a -> b
$ s -> s -> ScanState s s
forall s f. s -> f -> ScanState s f
ScanDo s
s s
fs1
                    FL.Done b
b -> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ScanState s s) b -> m (Step (ScanState s s) b))
-> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a b. (a -> b) -> a -> b
$ b -> ScanState s s -> Step (ScanState s s) b
forall s a. a -> s -> Step s a
Yield b
b ScanState s s
forall s f. ScanState s f
ScanDone
            Skip s
s -> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ScanState s s) b -> m (Step (ScanState s s) b))
-> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a b. (a -> b) -> a -> b
$ ScanState s s -> Step (ScanState s s) b
forall s a. s -> Step s a
Skip (ScanState s s -> Step (ScanState s s) b)
-> ScanState s s -> Step (ScanState s s) b
forall a b. (a -> b) -> a -> b
$ s -> s -> ScanState s s
forall s f. s -> f -> ScanState s f
ScanDo s
s s
fs
            Step s a
Stop -> s -> m b
final s
fs m b -> m (Step (ScanState s s) b) -> m (Step (ScanState s s) b)
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ScanState s s) b
forall s a. Step s a
Stop
    step State StreamK m a
_ ScanState s s
ScanDone = Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ScanState s s) b
forall s a. Step s a
Stop

{-# DEPRECATED postscan "Please use postscanl instead" #-}
{-# INLINE_NORMAL postscan #-}
postscan :: Monad m => FL.Fold m a b -> Stream m a -> Stream m b
postscan :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
postscan (FL.Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
extract s -> m b
final) =
    Scanl m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Scanl m a b -> Stream m a -> Stream m b
postscanl ((s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Scanl m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Scanl m a b
Scanl s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
extract s -> m b
final)

{-# INLINE scanlWith #-}
scanlWith :: Monad m
    => Bool -> Scanl m a b -> Stream m a -> Stream m b
scanlWith :: forall (m :: * -> *) a b.
Monad m =>
Bool -> Scanl m a b -> Stream m a -> Stream m b
scanlWith Bool
restart (Scanl s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
extract s -> m b
final) (Stream State StreamK m a -> s -> m (Step s a)
sstep s
state) =
    (State StreamK m b -> ScanState s s -> m (Step (ScanState s s) b))
-> ScanState s s -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> ScanState s s -> m (Step (ScanState s s) b)
forall {m :: * -> *} {a}.
State StreamK m a -> ScanState s s -> m (Step (ScanState s s) b)
step (s -> ScanState s s
forall s f. s -> ScanState s f
ScanInit s
state)

    where

    {-# INLINE runStep #-}
    runStep :: s -> m (Step s b) -> m (Step (ScanState s s) b)
runStep s
st m (Step s b)
action = do
        Step s b
res <- m (Step s b)
action
        case Step s b
res of
            FL.Partial s
fs -> do
                !b
b <- s -> m b
extract s
fs
                Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ScanState s s) b -> m (Step (ScanState s s) b))
-> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a b. (a -> b) -> a -> b
$ b -> ScanState s s -> Step (ScanState s s) b
forall s a. a -> s -> Step s a
Yield b
b (ScanState s s -> Step (ScanState s s) b)
-> ScanState s s -> Step (ScanState s s) b
forall a b. (a -> b) -> a -> b
$ s -> s -> ScanState s s
forall s f. s -> f -> ScanState s f
ScanDo s
st s
fs
            FL.Done b
b ->
                let next :: ScanState s f
next = if Bool
restart then s -> ScanState s f
forall s f. s -> ScanState s f
ScanInit s
st else ScanState s f
forall s f. ScanState s f
ScanDone
                 in Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ScanState s s) b -> m (Step (ScanState s s) b))
-> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a b. (a -> b) -> a -> b
$ b -> ScanState s s -> Step (ScanState s s) b
forall s a. a -> s -> Step s a
Yield b
b ScanState s s
forall {f}. ScanState s f
next

    {-# INLINE_LATE step #-}
    step :: State StreamK m a -> ScanState s s -> m (Step (ScanState s s) b)
step State StreamK m a
_ (ScanInit s
st) = s -> m (Step s b) -> m (Step (ScanState s s) b)
forall {s}. s -> m (Step s b) -> m (Step (ScanState s s) b)
runStep s
st m (Step s b)
initial
    step State StreamK m a
gst (ScanDo s
st s
fs) = do
        Step s a
res <- State StreamK m a -> s -> m (Step s a)
sstep (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
res of
            Yield a
x s
s -> s -> m (Step s b) -> m (Step (ScanState s s) b)
forall {s}. s -> m (Step s b) -> m (Step (ScanState s s) b)
runStep s
s (s -> a -> m (Step s b)
fstep s
fs a
x)
            Skip s
s -> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ScanState s s) b -> m (Step (ScanState s s) b))
-> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a b. (a -> b) -> a -> b
$ ScanState s s -> Step (ScanState s s) b
forall s a. s -> Step s a
Skip (ScanState s s -> Step (ScanState s s) b)
-> ScanState s s -> Step (ScanState s s) b
forall a b. (a -> b) -> a -> b
$ s -> s -> ScanState s s
forall s f. s -> f -> ScanState s f
ScanDo s
s s
fs
            Step s a
Stop -> s -> m b
final s
fs m b -> m (Step (ScanState s s) b) -> m (Step (ScanState s s) b)
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ScanState s s) b
forall s a. Step s a
Stop
    step State StreamK m a
_ ScanState s s
ScanDone = Step (ScanState s s) b -> m (Step (ScanState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ScanState s s) b
forall s a. Step s a
Stop

{-# DEPRECATED scanWith "Please use scanlWith instead" #-}
{-# INLINE scanWith #-}
scanWith :: Monad m
    => Bool -> Fold m a b -> Stream m a -> Stream m b
scanWith :: forall (m :: * -> *) a b.
Monad m =>
Bool -> Fold m a b -> Stream m a -> Stream m b
scanWith Bool
restart (Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
extract s -> m b
final) =
    Bool -> Scanl m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Bool -> Scanl m a b -> Stream m a -> Stream m b
scanlWith Bool
restart ((s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Scanl m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Scanl m a b
Scanl s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
extract s -> m b
final)

-- XXX It may be useful to have a version of scan where we can keep the
-- accumulator independent of the value emitted. So that we do not necessarily
-- have to keep a value in the accumulator which we are not using. We can pass
-- an extraction function that will take the accumulator and the current value
-- of the element and emit the next value in the stream. That will also make it
-- possible to modify the accumulator after using it. In fact, the step function
-- can return new accumulator and the value to be emitted. The signature would
-- be more like mapAccumL.

-- | Strict left scan. Scan a stream using the given fold. Scan includes
-- the initial (default) value of the accumulator as well as the final value.
-- Compare with 'postscan' which omits the initial value.
--
-- >>> s = Stream.fromList [1..10]
-- >>> Stream.fold Fold.toList $ Stream.takeWhile (< 10) $ Stream.scanl Scanl.sum s
-- [0,1,3,6]
--
-- See also: 'usingStateT'
--

-- EXPLANATION:
-- >>> scanl' step z = Stream.scanl (Scanl.mkScanl step z)
--
-- Like 'map', 'scanl'' too is a one to one transformation,
-- however it adds an extra element.
--
-- >>> s = Stream.fromList [1,2,3,4]
-- >>> Stream.fold Fold.toList $ scanl' (+) 0 s
-- [0,1,3,6,10]
--
-- >>> Stream.fold Fold.toList $ scanl' (flip (:)) [] s
-- [[],[1],[2,1],[3,2,1],[4,3,2,1]]
--
-- The output of 'scanl'' is the initial value of the accumulator followed by
-- all the intermediate steps and the final result of 'foldl''.
--
-- By streaming the accumulated state after each fold step, we can share the
-- state across multiple stages of stream composition. Each stage can modify or
-- extend the state, do some processing with it and emit it for the next stage,
-- thus modularizing the stream processing. This can be useful in
-- stateful or event-driven programming.
--
-- Consider the following monolithic example, computing the sum and the product
-- of the elements in a stream in one go using a @foldl'@:
--
-- >>> foldl' step z = Stream.fold (Scanl.mkScanl step z)
-- >>> foldl' (\(s, p) x -> (s + x, p * x)) (0,1) s
-- (10,24)
--
-- Using @scanl'@ we can make it modular by computing the sum in the first
-- stage and passing it down to the next stage for computing the product:
--
-- >>> :{
--   foldl' (\(_, p) (s, x) -> (s, p * x)) (0,1)
--   $ scanl' (\(s, _) x -> (s + x, x)) (0,1)
--   $ Stream.fromList [1,2,3,4]
-- :}
-- (10,24)
--
-- IMPORTANT: 'scanl'' evaluates the accumulator to WHNF.  To avoid building
-- lazy expressions inside the accumulator, it is recommended that a strict
-- data structure is used for accumulator.
--
{-# INLINE_NORMAL scanl #-}
scanl :: Monad m
    => Scanl m a b -> Stream m a -> Stream m b
scanl :: forall (m :: * -> *) a b.
Monad m =>
Scanl m a b -> Stream m a -> Stream m b
scanl = Bool -> Scanl m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Bool -> Scanl m a b -> Stream m a -> Stream m b
scanlWith Bool
False

-- | Like 'scanl' but restarts scanning afresh when the scanning fold
-- terminates.
--
{-# INLINE_NORMAL scanlMany #-}
scanlMany :: Monad m
    => Scanl m a b -> Stream m a -> Stream m b
scanlMany :: forall (m :: * -> *) a b.
Monad m =>
Scanl m a b -> Stream m a -> Stream m b
scanlMany = Bool -> Scanl m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Bool -> Scanl m a b -> Stream m a -> Stream m b
scanlWith Bool
True

{-# DEPRECATED scan "Please use scanl instead" #-}
{-# INLINE_NORMAL scan #-}
scan :: Monad m
    => FL.Fold m a b -> Stream m a -> Stream m b
scan :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
scan = Bool -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Bool -> Fold m a b -> Stream m a -> Stream m b
scanWith Bool
False

{-# DEPRECATED scanMany "Please use scanlMany instead" #-}
{-# INLINE_NORMAL scanMany #-}
scanMany :: Monad m
    => FL.Fold m a b -> Stream m a -> Stream m b
scanMany :: forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
scanMany = Bool -> Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Bool -> Fold m a b -> Stream m a -> Stream m b
scanWith Bool
True

------------------------------------------------------------------------------
-- Scanning - Prescans
------------------------------------------------------------------------------

-- Adapted from the vector package.
--
-- XXX Is a prescan useful, discarding the last step does not sound useful?  I
-- am not sure about the utility of this function, so this is implemented but
-- not exposed. We can expose it if someone provides good reasons why this is
-- useful.
--
-- XXX We have to execute the stream one step ahead to know that we are at the
-- last step.  The vector implementation of prescan executes the last fold step
-- but does not yield the result. This means we have executed the effect but
-- discarded value. This does not sound right. In this implementation we are
-- not executing the last fold step.
{-# INLINE_NORMAL prescanlM' #-}
prescanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
prescanlM' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
prescanlM' b -> a -> m b
f m b
mz (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m b -> (s, m b) -> m (Step (s, m b) b))
-> (s, m b) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> (s, m b) -> m (Step (s, m b) b)
forall {m :: * -> *} {a}.
State StreamK m a -> (s, m b) -> m (Step (s, m b) b)
step' (s
state, m b
mz)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, m b) -> m (Step (s, m b) b)
step' State StreamK m a
gst (s
st, m b
prev) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                b
acc <- m b
prev
                Step (s, m b) b -> m (Step (s, m b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, m b) b -> m (Step (s, m b) b))
-> Step (s, m b) b -> m (Step (s, m b) b)
forall a b. (a -> b) -> a -> b
$ b -> (s, m b) -> Step (s, m b) b
forall s a. a -> s -> Step s a
Yield b
acc (s
s, b -> a -> m b
f b
acc a
x)
            Skip s
s -> Step (s, m b) b -> m (Step (s, m b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, m b) b -> m (Step (s, m b) b))
-> Step (s, m b) b -> m (Step (s, m b) b)
forall a b. (a -> b) -> a -> b
$ (s, m b) -> Step (s, m b) b
forall s a. s -> Step s a
Skip (s
s, m b
prev)
            Step s a
Stop   -> Step (s, m b) b -> m (Step (s, m b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, m b) b
forall s a. Step s a
Stop

{-# INLINE prescanl' #-}
prescanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
prescanl' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> Stream m b
prescanl' b -> a -> b
f b
z = (b -> a -> m b) -> m b -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
prescanlM' (\b
a a
b -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> a -> b
f b
a a
b)) (b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
z)

------------------------------------------------------------------------------
-- Monolithic postscans (postscan followed by a map)
------------------------------------------------------------------------------

-- The performance of a modular postscan followed by a map seems to be
-- equivalent to this monolithic scan followed by map therefore we may not need
-- this implementation. We just have it for performance comparison and in case
-- modular version does not perform well in some situation.
--
{-# INLINE_NORMAL postscanlMx' #-}
postscanlMx' :: Monad m
    => (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
postscanlMx' :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
postscanlMx' x -> a -> m x
fstep m x
begin x -> m b
done (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = do
    (State StreamK m b -> (s, m x) -> m (Step (s, m x) b))
-> (s, m x) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> (s, m x) -> m (Step (s, m x) b)
forall {m :: * -> *} {a}.
State StreamK m a -> (s, m x) -> m (Step (s, m x) b)
step' (s
state, m x
begin)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, m x) -> m (Step (s, m x) b)
step' State StreamK m a
gst (s
st, m x
acc) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                x
old <- m x
acc
                x
y <- x -> a -> m x
fstep x
old a
x
                b
v <- x -> m b
done x
y
                b
v b -> m (Step (s, m x) b) -> m (Step (s, m x) b)
forall a b. a -> b -> b
`seq` x
y x -> m (Step (s, m x) b) -> m (Step (s, m x) b)
forall a b. a -> b -> b
`seq` Step (s, m x) b -> m (Step (s, m x) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> (s, m x) -> Step (s, m x) b
forall s a. a -> s -> Step s a
Yield b
v (s
s, x -> m x
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return x
y))
            Skip s
s -> Step (s, m x) b -> m (Step (s, m x) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, m x) b -> m (Step (s, m x) b))
-> Step (s, m x) b -> m (Step (s, m x) b)
forall a b. (a -> b) -> a -> b
$ (s, m x) -> Step (s, m x) b
forall s a. s -> Step s a
Skip (s
s, m x
acc)
            Step s a
Stop   -> Step (s, m x) b -> m (Step (s, m x) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, m x) b
forall s a. Step s a
Stop

{-# INLINE_NORMAL postscanlx' #-}
postscanlx' :: Monad m
    => (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
postscanlx' :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
postscanlx' x -> a -> x
fstep x
begin x -> b
done =
    (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
postscanlMx' (\x
b a
a -> x -> m x
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> a -> x
fstep x
b a
a)) (x -> m x
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return x
begin) (b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (x -> b) -> x -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> b
done)

-- XXX do we need consM strict to evaluate the begin value?
{-# INLINE scanlMx' #-}
scanlMx' :: Monad m
    => (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
scanlMx' :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
scanlMx' x -> a -> m x
fstep m x
begin x -> m b
done Stream m a
s =
    (m x
begin m x -> (x -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
x -> x
x x -> m b -> m b
forall a b. a -> b -> b
`seq` x -> m b
done x
x) m b -> Stream m b -> Stream m b
forall (m :: * -> *) a.
Applicative m =>
m a -> Stream m a -> Stream m a
`consM` (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
postscanlMx' x -> a -> m x
fstep m x
begin x -> m b
done Stream m a
s

{-# INLINE scanlx' #-}
scanlx' :: Monad m
    => (x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
scanlx' :: forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Stream m a -> Stream m b
scanlx' x -> a -> x
fstep x
begin x -> b
done =
    (x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> m x) -> m x -> (x -> m b) -> Stream m a -> Stream m b
scanlMx' (\x
b a
a -> x -> m x
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> a -> x
fstep x
b a
a)) (x -> m x
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return x
begin) (b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (x -> b) -> x -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> b
done)

------------------------------------------------------------------------------
-- postscans
------------------------------------------------------------------------------

-- Adapted from the vector package.
{-# INLINE_NORMAL postscanlM' #-}
postscanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
postscanlM' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
postscanlM' b -> a -> m b
fstep m b
begin (Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
    (State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b))
-> Maybe (s, b) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
forall {m :: * -> *} {a}.
State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' Maybe (s, b)
forall a. Maybe a
Nothing
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' State StreamK m a
_ Maybe (s, b)
Nothing = do
        !b
x <- m b
begin
        Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. s -> Step s a
Skip ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
state, b
x))

    step' State StreamK m a
gst (Just (s
st, b
acc)) =  do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                !b
y <- b -> a -> m b
fstep b
acc a
x
                Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ b -> Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. a -> s -> Step s a
Yield b
y ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
y))
            Skip s
s -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. s -> Step s a
Skip ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
acc))
            Step s a
Stop   -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe (s, b)) b
forall s a. Step s a
Stop

{-# INLINE_NORMAL postscanl' #-}
postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
postscanl' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> Stream m b
postscanl' a -> b -> a
f a
seed = (a -> b -> m a) -> m a -> Stream m b -> Stream m a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
postscanlM' (\a
a b
b -> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b)) (a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
seed)

{-# ANN type PScanAfterState Fuse #-}
data PScanAfterState m st acc =
      PScanAfterStep st (m acc)
    | PScanAfterYield acc (PScanAfterState m st acc)
    | PScanAfterStop

-- We can possibly have the "done" function as a Maybe to provide an option to
-- emit or not emit the accumulator when the stream stops.
--
-- TBD: use a single Yield point
--
{-# INLINE_NORMAL postscanlMAfter' #-}
postscanlMAfter' :: Monad m
    => (b -> a -> m b) -> m b -> (b -> m b) -> Stream m a -> Stream m b
postscanlMAfter' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> (b -> m b) -> Stream m a -> Stream m b
postscanlMAfter' b -> a -> m b
fstep m b
initial b -> m b
done (Stream State StreamK m a -> s -> m (Step s a)
step1 s
state1) = do
    (State StreamK m b
 -> PScanAfterState m s b -> m (Step (PScanAfterState m s b) b))
-> PScanAfterState m s b -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b
-> PScanAfterState m s b -> m (Step (PScanAfterState m s b) b)
forall {m :: * -> *} {a}.
State StreamK m a
-> PScanAfterState m s b -> m (Step (PScanAfterState m s b) b)
step (s -> m b -> PScanAfterState m s b
forall (m :: * -> *) st acc.
st -> m acc -> PScanAfterState m st acc
PScanAfterStep s
state1 m b
initial)

    where

    {-# INLINE_LATE step #-}
    step :: State StreamK m a
-> PScanAfterState m s b -> m (Step (PScanAfterState m s b) b)
step State StreamK m a
gst (PScanAfterStep s
st m b
acc) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                !b
old <- m b
acc
                !b
y <- b -> a -> m b
fstep b
old a
x
                Step (PScanAfterState m s b) b
-> m (Step (PScanAfterState m s b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (PScanAfterState m s b -> Step (PScanAfterState m s b) b
forall s a. s -> Step s a
Skip (PScanAfterState m s b -> Step (PScanAfterState m s b) b)
-> PScanAfterState m s b -> Step (PScanAfterState m s b) b
forall a b. (a -> b) -> a -> b
$ b -> PScanAfterState m s b -> PScanAfterState m s b
forall (m :: * -> *) st acc.
acc -> PScanAfterState m st acc -> PScanAfterState m st acc
PScanAfterYield b
y (s -> m b -> PScanAfterState m s b
forall (m :: * -> *) st acc.
st -> m acc -> PScanAfterState m st acc
PScanAfterStep s
s (b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
y)))
            Skip s
s -> Step (PScanAfterState m s b) b
-> m (Step (PScanAfterState m s b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (PScanAfterState m s b) b
 -> m (Step (PScanAfterState m s b) b))
-> Step (PScanAfterState m s b) b
-> m (Step (PScanAfterState m s b) b)
forall a b. (a -> b) -> a -> b
$ PScanAfterState m s b -> Step (PScanAfterState m s b) b
forall s a. s -> Step s a
Skip (PScanAfterState m s b -> Step (PScanAfterState m s b) b)
-> PScanAfterState m s b -> Step (PScanAfterState m s b) b
forall a b. (a -> b) -> a -> b
$ s -> m b -> PScanAfterState m s b
forall (m :: * -> *) st acc.
st -> m acc -> PScanAfterState m st acc
PScanAfterStep s
s m b
acc
            -- Strictness is important for fusion
            Step s a
Stop -> do
                !b
v <- m b
acc
                !b
res <- b -> m b
done b
v
                Step (PScanAfterState m s b) b
-> m (Step (PScanAfterState m s b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (PScanAfterState m s b -> Step (PScanAfterState m s b) b
forall s a. s -> Step s a
Skip (PScanAfterState m s b -> Step (PScanAfterState m s b) b)
-> PScanAfterState m s b -> Step (PScanAfterState m s b) b
forall a b. (a -> b) -> a -> b
$ b -> PScanAfterState m s b -> PScanAfterState m s b
forall (m :: * -> *) st acc.
acc -> PScanAfterState m st acc -> PScanAfterState m st acc
PScanAfterYield b
res PScanAfterState m s b
forall (m :: * -> *) st acc. PScanAfterState m st acc
PScanAfterStop)
    step State StreamK m a
_ (PScanAfterYield b
acc PScanAfterState m s b
next) = Step (PScanAfterState m s b) b
-> m (Step (PScanAfterState m s b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (PScanAfterState m s b) b
 -> m (Step (PScanAfterState m s b) b))
-> Step (PScanAfterState m s b) b
-> m (Step (PScanAfterState m s b) b)
forall a b. (a -> b) -> a -> b
$ b -> PScanAfterState m s b -> Step (PScanAfterState m s b) b
forall s a. a -> s -> Step s a
Yield b
acc PScanAfterState m s b
next
    step State StreamK m a
_ PScanAfterState m s b
PScanAfterStop = Step (PScanAfterState m s b) b
-> m (Step (PScanAfterState m s b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (PScanAfterState m s b) b
forall s a. Step s a
Stop

{-# INLINE_NORMAL postscanlM #-}
postscanlM :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
postscanlM :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
postscanlM b -> a -> m b
fstep m b
begin (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b))
-> Maybe (s, b) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
forall {m :: * -> *} {a}.
State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' Maybe (s, b)
forall a. Maybe a
Nothing
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' State StreamK m a
_ Maybe (s, b)
Nothing = do
        b
r <- m b
begin
        Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. s -> Step s a
Skip ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
state, b
r))

    step' State StreamK m a
gst (Just (s
st, b
acc)) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                b
y <- b -> a -> m b
fstep b
acc a
x
                Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. a -> s -> Step s a
Yield b
y ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
y)))
            Skip s
s -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. s -> Step s a
Skip ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
acc))
            Step s a
Stop   -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe (s, b)) b
forall s a. Step s a
Stop

{-# INLINE_NORMAL postscanlBy #-}
postscanlBy :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
postscanlBy :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> Stream m b
postscanlBy a -> b -> a
f a
seed = (a -> b -> m a) -> m a -> Stream m b -> Stream m a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
postscanlM (\a
a b
b -> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b)) (a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
seed)

-- | Like 'scanl'' but with a monadic step function and a monadic seed.
--
{-# INLINE_NORMAL scanlM' #-}
scanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
scanlM' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
scanlM' b -> a -> m b
fstep m b
begin (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b))
-> Maybe (s, b) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
forall {m :: * -> *} {a}.
State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' Maybe (s, b)
forall a. Maybe a
Nothing
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' State StreamK m a
_ Maybe (s, b)
Nothing = do
        !b
x <- m b
begin
        Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ b -> Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. a -> s -> Step s a
Yield b
x ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
state, b
x))
    step' State StreamK m a
gst (Just (s
st, b
acc)) =  do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                !b
y <- b -> a -> m b
fstep b
acc a
x
                Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ b -> Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. a -> s -> Step s a
Yield b
y ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
y))
            Skip s
s -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. s -> Step s a
Skip ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
acc))
            Step s a
Stop   -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe (s, b)) b
forall s a. Step s a
Stop

-- | @scanlMAfter' accumulate initial done stream@ is like 'scanlM'' except
-- that it provides an additional @done@ function to be applied on the
-- accumulator when the stream stops. The result of @done@ is also emitted in
-- the stream.
--
-- This function can be used to allocate a resource in the beginning of the
-- scan and release it when the stream ends or to flush the internal state of
-- the scan at the end.
--
-- /Pre-release/
--
{-# INLINE scanlMAfter' #-}
scanlMAfter' :: Monad m
    => (b -> a -> m b) -> m b -> (b -> m b) -> Stream m a -> Stream m b
scanlMAfter' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> (b -> m b) -> Stream m a -> Stream m b
scanlMAfter' b -> a -> m b
fstep m b
initial b -> m b
done Stream m a
s =
    m b
initial m b -> Stream m b -> Stream m b
forall (m :: * -> *) a.
Applicative m =>
m a -> Stream m a -> Stream m a
`consM` (b -> a -> m b) -> m b -> (b -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> (b -> m b) -> Stream m a -> Stream m b
postscanlMAfter' b -> a -> m b
fstep m b
initial b -> m b
done Stream m a
s

-- >>> scanl' f z xs = z `Stream.cons` postscanl' f z xs

-- | Strict left scan. Like 'map', 'scanl'' too is a one to one transformation,
-- however it adds an extra element.
--
-- >>> Stream.toList $ Stream.scanl' (+) 0 $ Stream.fromList [1,2,3,4]
-- [0,1,3,6,10]
--
-- >>> Stream.toList $ Stream.scanl' (flip (:)) [] $ Stream.fromList [1,2,3,4]
-- [[],[1],[2,1],[3,2,1],[4,3,2,1]]
--
-- The output of 'scanl'' is the initial value of the accumulator followed by
-- all the intermediate steps and the final result of 'foldl''.
--
-- By streaming the accumulated state after each fold step, we can share the
-- state across multiple stages of stream composition. Each stage can modify or
-- extend the state, do some processing with it and emit it for the next stage,
-- thus modularizing the stream processing. This can be useful in
-- stateful or event-driven programming.
--
-- Consider the following monolithic example, computing the sum and the product
-- of the elements in a stream in one go using a @foldl'@:
--
-- >>> Stream.fold (Fold.foldl' (\(s, p) x -> (s + x, p * x)) (0,1)) $ Stream.fromList [1,2,3,4]
-- (10,24)
--
-- Using @scanl'@ we can make it modular by computing the sum in the first
-- stage and passing it down to the next stage for computing the product:
--
-- >>> :{
--   Stream.fold (Fold.foldl' (\(_, p) (s, x) -> (s, p * x)) (0,1))
--   $ Stream.scanl' (\(s, _) x -> (s + x, x)) (0,1)
--   $ Stream.fromList [1,2,3,4]
-- :}
-- (10,24)
--
-- IMPORTANT: 'scanl'' evaluates the accumulator to WHNF.  To avoid building
-- lazy expressions inside the accumulator, it is recommended that a strict
-- data structure is used for accumulator.
--
-- >>> scanl' step z = Stream.scanl (Scanl.mkScanl step z)
-- >>> scanl' f z xs = Stream.scanlM' (\a b -> return (f a b)) (return z) xs
--
-- See also: 'usingStateT'
--
{-# INLINE scanl' #-}
scanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
scanl' :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> Stream m b
scanl' b -> a -> b
f b
seed = (b -> a -> m b) -> m b -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
scanlM' (\b
a a
b -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> a -> b
f b
a a
b)) (b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
seed)

{-# INLINE_NORMAL scanlM #-}
scanlM :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
scanlM :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
scanlM b -> a -> m b
fstep m b
begin (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b))
-> Maybe (s, b) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
forall {m :: * -> *} {a}.
State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' Maybe (s, b)
forall a. Maybe a
Nothing
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> Maybe (s, b) -> m (Step (Maybe (s, b)) b)
step' State StreamK m a
_ Maybe (s, b)
Nothing = do
        b
x <- m b
begin
        Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ b -> Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. a -> s -> Step s a
Yield b
x ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
state, b
x))
    step' State StreamK m a
gst (Just (s
st, b
acc)) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> do
                b
y <- b -> a -> m b
fstep b
acc a
x
                Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ b -> Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. a -> s -> Step s a
Yield b
y ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
y))
            Skip s
s -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b))
-> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a b. (a -> b) -> a -> b
$ Maybe (s, b) -> Step (Maybe (s, b)) b
forall s a. s -> Step s a
Skip ((s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just (s
s, b
acc))
            Step s a
Stop   -> Step (Maybe (s, b)) b -> m (Step (Maybe (s, b)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe (s, b)) b
forall s a. Step s a
Stop

{-# INLINE scanlBy #-}
scanlBy :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
scanlBy :: forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> Stream m b
scanlBy b -> a -> b
f b
seed = (b -> a -> m b) -> m b -> Stream m a -> Stream m b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> Stream m b
scanlM (\b
a a
b -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> a -> b
f b
a a
b)) (b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
seed)

-- Adapted from the vector package
{-# INLINE_NORMAL scanl1M #-}
scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
scanl1M :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M a -> a -> m a
fstep (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a))
-> (s, Maybe a) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
state, Maybe a
forall a. Maybe a
Nothing)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a)
step' State StreamK m a
gst (s
st, Maybe a
Nothing) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
x (s
s, a -> Maybe a
forall a. a -> Maybe a
Just a
x)
            Skip s
s -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, Maybe a
forall a. Maybe a
Nothing)
            Step s a
Stop   -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Stop

    step' State StreamK m a
gst (s
st, Just a
acc) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
y s
s -> do
                a
z <- a -> a -> m a
fstep a
acc a
y
                Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
z (s
s, a -> Maybe a
forall a. a -> Maybe a
Just a
z)
            Skip s
s -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, a -> Maybe a
forall a. a -> Maybe a
Just a
acc)
            Step s a
Stop   -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Stop

{-# INLINE scanl1 #-}
scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
scanl1 :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> Stream m a
scanl1 a -> a -> a
f = (a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M (\a
x a
y -> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- Adapted from the vector package

-- | Like 'scanl1'' but with a monadic step function.
--
{-# INLINE_NORMAL scanl1M' #-}
scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' a -> a -> m a
fstep (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a))
-> (s, Maybe a) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
state, Maybe a
forall a. Maybe a
Nothing)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a)
step' State StreamK m a
gst (s
st, Maybe a
Nothing) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> a
x a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
forall a b. a -> b -> b
`seq` Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
x (s
s, a -> Maybe a
forall a. a -> Maybe a
Just a
x)
            Skip s
s -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, Maybe a
forall a. Maybe a
Nothing)
            Step s a
Stop   -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Stop

    step' State StreamK m a
gst (s
st, Just a
acc) = a
acc a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
forall a b. a -> b -> b
`seq` do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
y s
s -> do
                a
z <- a -> a -> m a
fstep a
acc a
y
                a
z a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
forall a b. a -> b -> b
`seq` Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
z (s
s, a -> Maybe a
forall a. a -> Maybe a
Just a
z)
            Skip s
s -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, a -> Maybe a
forall a. a -> Maybe a
Just a
acc)
            Step s a
Stop   -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Stop

-- | Like 'scanl'' but for a non-empty stream. The first element of the stream
-- is used as the initial value of the accumulator. Does nothing if the stream
-- is empty.
--
-- >>> Stream.toList $ Stream.scanl1' (+) $ Stream.fromList [1,2,3,4]
-- [1,3,6,10]
--
{-# INLINE scanl1' #-}
scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
scanl1' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> Stream m a
scanl1' a -> a -> a
f = (a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' (\a
x a
y -> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-------------------------------------------------------------------------------
-- Filtering
-------------------------------------------------------------------------------

-- | Modify a @Stream m a -> Stream m a@ stream transformation that accepts a
-- predicate @(a -> b)@ to accept @((s, a) -> b)@ instead, provided a
-- transformation @Stream m a -> Stream m (s, a)@. Convenient to filter with
-- index or time.
--
-- >>> filterWithIndex = Stream.with Stream.indexed Stream.filter
--
-- /Pre-release/
{-# INLINE with #-}
with :: Monad m =>
       (Stream m a -> Stream m (s, a))
    -> (((s, a) -> b) -> Stream m (s, a) -> Stream m (s, a))
    -> (((s, a) -> b) -> Stream m a -> Stream m a)
with :: forall (m :: * -> *) a s b.
Monad m =>
(Stream m a -> Stream m (s, a))
-> (((s, a) -> b) -> Stream m (s, a) -> Stream m (s, a))
-> ((s, a) -> b)
-> Stream m a
-> Stream m a
with Stream m a -> Stream m (s, a)
f ((s, a) -> b) -> Stream m (s, a) -> Stream m (s, a)
comb (s, a) -> b
g = ((s, a) -> a) -> Stream m (s, a) -> Stream m a
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (s, a) -> a
forall a b. (a, b) -> b
snd (Stream m (s, a) -> Stream m a)
-> (Stream m a -> Stream m (s, a)) -> Stream m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((s, a) -> b) -> Stream m (s, a) -> Stream m (s, a)
comb (s, a) -> b
g (Stream m (s, a) -> Stream m (s, a))
-> (Stream m a -> Stream m (s, a)) -> Stream m a -> Stream m (s, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stream m a -> Stream m (s, a)
f

-- Adapted from the vector package

-- | Same as 'filter' but with a monadic predicate.
--
-- >>> f p x = p x >>= \r -> return $ if r then Just x else Nothing
-- >>> filterM p = Stream.mapMaybeM (f p)
--
{-# INLINE_NORMAL filterM #-}
filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
filterM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
filterM a -> m Bool
f (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
step' s
state
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> s -> m (Step s a)
step' State StreamK m a
gst s
st = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> do
                Bool
b <- a -> m Bool
f a
x
                Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ if Bool
b
                         then a -> s -> Step s a
forall s a. a -> s -> Step s a
Yield a
x s
s
                         else s -> Step s a
forall s a. s -> Step s a
Skip s
s
            Skip s
s -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s
            Step s a
Stop   -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s a
forall s a. Step s a
Stop

-- | Include only those elements that pass a predicate.
--
-- >>> filter p = Stream.filterM (return . p)
-- >>> filter p = Stream.mapMaybe (\x -> if p x then Just x else Nothing)
-- >>> filter p = Stream.postscanlMaybe (Scanl.filtering p)
--
{-# INLINE filter #-}
filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
filter :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
filter a -> Bool
f = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
filterM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
-- filter p = scanMaybe (FL.filtering p)

-- | Drop repeated elements that are adjacent to each other using the supplied
-- comparison function.
--
-- >>> uniq = Stream.uniqBy (==)
--
-- To strip duplicate path separators:
--
-- >>> input = Stream.fromList "//a//b"
-- >>> f x y = x == '/' && y == '/'
-- >>> Stream.fold Fold.toList $ Stream.uniqBy f input
-- "/a/b"
--
-- Space: @O(1)@
--
-- /Pre-release/
--
{-# INLINE uniqBy #-}
uniqBy :: Monad m =>
    (a -> a -> Bool) -> Stream m a -> Stream m a
-- uniqBy eq = scanMaybe (FL.uniqBy eq)
uniqBy :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> Bool) -> Stream m a -> Stream m a
uniqBy a -> a -> Bool
eq = Stream m (Maybe a) -> Stream m a
forall (m :: * -> *) a. Monad m => Stream m (Maybe a) -> Stream m a
catMaybes (Stream m (Maybe a) -> Stream m a)
-> (Stream m a -> Stream m (Maybe a)) -> Stream m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> a -> Maybe a) -> Stream m a -> Stream m (Maybe a)
forall (m :: * -> *) a b.
Monad m =>
(Maybe a -> a -> b) -> Stream m a -> Stream m b
rollingMap Maybe a -> a -> Maybe a
f

    where

    f :: Maybe a -> a -> Maybe a
f Maybe a
pre a
curr =
        case Maybe a
pre of
            Maybe a
Nothing -> a -> Maybe a
forall a. a -> Maybe a
Just a
curr
            Just a
x -> if a
x a -> a -> Bool
`eq` a
curr then Maybe a
forall a. Maybe a
Nothing else a -> Maybe a
forall a. a -> Maybe a
Just a
curr

-- Adapted from the vector package

-- | Drop repeated elements that are adjacent to each other.
--
-- >>> uniq = Stream.uniqBy (==)
--
{-# INLINE_NORMAL uniq #-}
uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
-- uniq = scanMaybe FL.uniq
uniq :: forall a (m :: * -> *). (Eq a, Monad m) => Stream m a -> Stream m a
uniq (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a))
-> (Maybe a, s) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
forall a. Maybe a
Nothing, s
state)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
step' State StreamK m a
gst (Maybe a
Nothing, s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ a -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s)
            Skip  s
s   -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip  (Maybe a
forall a. Maybe a
Nothing, s
s)
            Step s a
Stop      -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe a, s) a
forall s a. Step s a
Stop
    step' State StreamK m a
gst (Just a
x, s
st)  = do
         Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
         case Step s a
r of
             Yield a
y s
s | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y   -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s)
                       | Bool
otherwise -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ a -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. a -> s -> Step s a
Yield a
y (a -> Maybe a
forall a. a -> Maybe a
Just a
y, s
s)
             Skip  s
s   -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s)
             Step s a
Stop      -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Maybe a, s) a
forall s a. Step s a
Stop

-- | Deletes the first occurrence of the element in the stream that satisfies
-- the given equality predicate.
--
-- >>> input = Stream.fromList [1,3,3,5]
-- >>> Stream.fold Fold.toList $ Stream.deleteBy (==) 3 input
-- [1,3,5]
--
{-# INLINE_NORMAL deleteBy #-}
deleteBy :: Monad m => (a -> a -> Bool) -> a -> Stream m a -> Stream m a
-- deleteBy cmp x = scanMaybe (FL.deleteBy cmp x)
deleteBy :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> Bool) -> a -> Stream m a -> Stream m a
deleteBy a -> a -> Bool
eq a
x (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a -> (s, Bool) -> m (Step (s, Bool) a))
-> (s, Bool) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (s, Bool) -> m (Step (s, Bool) a)
step' (s
state, Bool
False)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, Bool) -> m (Step (s, Bool) a)
step' State StreamK m a
gst (s
st, Bool
False) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
y s
s -> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool) a -> m (Step (s, Bool) a))
-> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a b. (a -> b) -> a -> b
$
                if a -> a -> Bool
eq a
x a
y then (s, Bool) -> Step (s, Bool) a
forall s a. s -> Step s a
Skip (s
s, Bool
True) else a -> (s, Bool) -> Step (s, Bool) a
forall s a. a -> s -> Step s a
Yield a
y (s
s, Bool
False)
            Skip s
s -> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool) a -> m (Step (s, Bool) a))
-> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a b. (a -> b) -> a -> b
$ (s, Bool) -> Step (s, Bool) a
forall s a. s -> Step s a
Skip (s
s, Bool
False)
            Step s a
Stop   -> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Bool) a
forall s a. Step s a
Stop

    step' State StreamK m a
gst (s
st, Bool
True) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
y s
s -> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool) a -> m (Step (s, Bool) a))
-> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Bool) -> Step (s, Bool) a
forall s a. a -> s -> Step s a
Yield a
y (s
s, Bool
True)
            Skip s
s -> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool) a -> m (Step (s, Bool) a))
-> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a b. (a -> b) -> a -> b
$ (s, Bool) -> Step (s, Bool) a
forall s a. s -> Step s a
Skip (s
s, Bool
True)
            Step s a
Stop   -> Step (s, Bool) a -> m (Step (s, Bool) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Bool) a
forall s a. Step s a
Stop

-- | Strip all leading and trailing occurrences of an element passing a
-- predicate and make all other consecutive occurrences uniq.
--
-- >> prune p = Stream.dropWhileAround p $ Stream.uniqBy (x y -> p x && p y)
--
-- @
-- > Stream.prune isSpace (Stream.fromList "  hello      world!   ")
-- "hello world!"
--
-- @
--
-- Space: @O(1)@
--
-- /Unimplemented/
{-# INLINE prune #-}
prune ::
    -- (Monad m, Eq a) =>
    (a -> Bool) -> Stream m a -> Stream m a
prune :: forall a (m :: * -> *). (a -> Bool) -> Stream m a -> Stream m a
prune = [Char] -> (a -> Bool) -> Stream m a -> Stream m a
forall a. HasCallStack => [Char] -> a
error [Char]
"Not implemented yet!"

-- Possible implementation:
-- @repeated =
--      Stream.catMaybes . Stream.parseMany (Parser.groupBy (==) Fold.repeated)@
--
-- 'Fold.repeated' should return 'Just' when repeated, and 'Nothing' for a
-- single element.

-- | Emit only repeated elements, once.
--
-- /Unimplemented/
repeated :: -- (Monad m, Eq a) =>
    Stream m a -> Stream m a
repeated :: forall (m :: * -> *) a. Stream m a -> Stream m a
repeated = Stream m a -> Stream m a
forall a. HasCallStack => a
undefined

------------------------------------------------------------------------------
-- Sampling
------------------------------------------------------------------------------

-- XXX We can implement this using addition instead of "mod" to make it more
-- efficient.

-- | @sampleFromThen offset stride@ takes the element at @offset@ index and
-- then every element at strides of @stride@.
--
-- >>> Stream.fold Fold.toList $ Stream.sampleFromThen 2 3 $ Stream.enumerateFromTo 0 10
-- [2,5,8]
--
{-# INLINE sampleFromThen #-}
sampleFromThen, strideFromThen :: Monad m =>
    Int -> Int -> Stream m a -> Stream m a
sampleFromThen :: forall (m :: * -> *) a.
Monad m =>
Int -> Int -> Stream m a -> Stream m a
sampleFromThen Int
offset Int
stride =
    (Stream m a -> Stream m (Int, a))
-> (((Int, a) -> Bool) -> Stream m (Int, a) -> Stream m (Int, a))
-> ((Int, a) -> Bool)
-> Stream m a
-> Stream m a
forall (m :: * -> *) a s b.
Monad m =>
(Stream m a -> Stream m (s, a))
-> (((s, a) -> b) -> Stream m (s, a) -> Stream m (s, a))
-> ((s, a) -> b)
-> Stream m a
-> Stream m a
with Stream m a -> Stream m (Int, a)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
indexed ((Int, a) -> Bool) -> Stream m (Int, a) -> Stream m (Int, a)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
filter
        (\(Int
i, a
_) -> Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
offset Bool -> Bool -> Bool
&& (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
offset) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
stride Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)

RENAME(strideFromThen,sampleFromThen)

------------------------------------------------------------------------------
-- Trimming
------------------------------------------------------------------------------

-- | init for non-empty streams, fails for empty stream case.
--
{-# INLINE initNonEmpty #-}
initNonEmpty :: Monad m => Stream m a -> Stream m a
initNonEmpty :: forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
initNonEmpty (Stream State StreamK m a -> s -> m (Step s a)
step1 s
state1) = (State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a))
-> (Maybe a, s) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
forall {m :: * -> *} {a}.
State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
step (Maybe a
forall a. Maybe a
Nothing, s
state1)

    where

    step :: State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
step State StreamK m a
gst (Maybe a
Nothing, s
s1) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
s1
        Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$
            case Step s a
r of
                Yield a
x s
s -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s)
                Skip s
s -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (Maybe a
forall a. Maybe a
Nothing, s
s)
                Step s a
Stop -> [Char] -> Step (Maybe a, s) a
forall a. HasCallStack => [Char] -> a
error [Char]
"initNonEmpty: empty Stream"

    step State StreamK m a
gst (Just a
a, s
s1) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
s1
        Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$
            case Step s a
r of
                Yield a
x s
s -> a -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. a -> s -> Step s a
Yield a
a (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s)
                Skip s
s -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
a, s
s)
                Step s a
Stop -> Step (Maybe a, s) a
forall s a. Step s a
Stop

-- | tail for non-empty streams, fails for empty stream case.
--
-- See also 'tail' for a non-partial version of this function..
{-# INLINE tailNonEmpty #-}
tailNonEmpty :: Monad m => Stream m a -> Stream m a
tailNonEmpty :: forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
tailNonEmpty (Stream State StreamK m a -> s -> m (Step s a)
step1 s
state1) = (State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a))
-> (Maybe a, s) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
forall {m :: * -> *} {a}.
State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
step (Maybe a
forall a. Maybe a
Nothing, s
state1)

    where

    step :: State StreamK m a -> (Maybe a, s) -> m (Step (Maybe a, s) a)
step State StreamK m a
gst (Maybe a
Nothing, s
s1) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
s1
        Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$
            case Step s a
r of
                Yield a
x s
s -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s)
                Skip s
s -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (Maybe a
forall a. Maybe a
Nothing, s
s)
                Step s a
Stop -> [Char] -> Step (Maybe a, s) a
forall a. HasCallStack => [Char] -> a
error [Char]
"tailNonEmpty: empty Stream"

    step State StreamK m a
gst (Just a
a, s
s1) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
s1
        Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$
            case Step s a
r of
                Yield a
x s
s -> a -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s)
                Skip s
s -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
a, s
s)
                Step s a
Stop -> Step (Maybe a, s) a
forall s a. Step s a
Stop

-- | Take all consecutive elements at the end of the stream for which the
-- predicate is true.
--
-- O(n) space, where n is the number elements taken.
--
-- /Unimplemented/
{-# INLINE takeWhileLast #-}
takeWhileLast :: -- Monad m =>
    (a -> Bool) -> Stream m a -> Stream m a
takeWhileLast :: forall a (m :: * -> *). (a -> Bool) -> Stream m a -> Stream m a
takeWhileLast = (a -> Bool) -> Stream m a -> Stream m a
forall a. HasCallStack => a
undefined -- fromStreamD $ D.takeWhileLast n $ toStreamD m

-- | Like 'takeWhile' and 'takeWhileLast' combined.
--
-- O(n) space, where n is the number elements taken from the end.
--
-- /Unimplemented/
{-# INLINE takeWhileAround #-}
takeWhileAround :: -- Monad m =>
    (a -> Bool) -> Stream m a -> Stream m a
takeWhileAround :: forall a (m :: * -> *). (a -> Bool) -> Stream m a -> Stream m a
takeWhileAround = (a -> Bool) -> Stream m a -> Stream m a
forall a. HasCallStack => a
undefined -- fromStreamD $ D.takeWhileAround n $ toStreamD m

-- Adapted from the vector package

-- | Discard first 'n' elements from the stream and take the rest.
--
{-# INLINE_NORMAL drop #-}
drop :: Monad m => Int -> Stream m a -> Stream m a
drop :: forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
drop Int
n (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a -> (s, Maybe Int) -> m (Step (s, Maybe Int) a))
-> (s, Maybe Int) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> (s, Maybe Int) -> m (Step (s, Maybe Int) a)
forall {a}.
(Ord a, Num a) =>
State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
state, Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, Maybe a) -> m (Step (s, Maybe a) a)
step' State StreamK m a
gst (s
st, Just a
i)
      | a
i a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 = do
          Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
          Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$
            case Step s a
r of
              Yield a
_ s
s -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, a -> Maybe a
forall a. a -> Maybe a
Just (a
i a -> a -> a
forall a. Num a => a -> a -> a
- a
1))
              Skip s
s    -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, a -> Maybe a
forall a. a -> Maybe a
Just a
i)
              Step s a
Stop      -> Step (s, Maybe a) a
forall s a. Step s a
Stop
      | Bool
otherwise = Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
st, Maybe a
forall a. Maybe a
Nothing)

    step' State StreamK m a
gst (s
st, Maybe a
Nothing) = do
      Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
      Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$
        case Step s a
r of
          Yield a
x s
s -> a -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
x (s
s, Maybe a
forall a. Maybe a
Nothing)
          Skip  s
s   -> (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, Maybe a
forall a. Maybe a
Nothing)
          Step s a
Stop      -> Step (s, Maybe a) a
forall s a. Step s a
Stop

-- Adapted from the vector package
data DropWhileState s a
    = DropWhileDrop s
    | DropWhileYield a s
    | DropWhileNext s

-- | Same as 'dropWhile' but with a monadic predicate.
--
{-# INLINE_NORMAL dropWhileM #-}
dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
-- dropWhileM p = scanMaybe (FL.droppingWhileM p)
dropWhileM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
dropWhileM a -> m Bool
f (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a
 -> DropWhileState s a -> m (Step (DropWhileState s a) a))
-> DropWhileState s a -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a
-> DropWhileState s a -> m (Step (DropWhileState s a) a)
forall {a}.
State StreamK m a
-> DropWhileState s a -> m (Step (DropWhileState s a) a)
step' (s -> DropWhileState s a
forall s a. s -> DropWhileState s a
DropWhileDrop s
state)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a
-> DropWhileState s a -> m (Step (DropWhileState s a) a)
step' State StreamK m a
gst (DropWhileDrop s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> do
                Bool
b <- a -> m Bool
f a
x
                if Bool
b
                then Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a))
-> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a b. (a -> b) -> a -> b
$ DropWhileState s a -> Step (DropWhileState s a) a
forall s a. s -> Step s a
Skip (s -> DropWhileState s a
forall s a. s -> DropWhileState s a
DropWhileDrop s
s)
                else Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a))
-> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a b. (a -> b) -> a -> b
$ DropWhileState s a -> Step (DropWhileState s a) a
forall s a. s -> Step s a
Skip (a -> s -> DropWhileState s a
forall s a. a -> s -> DropWhileState s a
DropWhileYield a
x s
s)
            Skip s
s -> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a))
-> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a b. (a -> b) -> a -> b
$ DropWhileState s a -> Step (DropWhileState s a) a
forall s a. s -> Step s a
Skip (s -> DropWhileState s a
forall s a. s -> DropWhileState s a
DropWhileDrop s
s)
            Step s a
Stop -> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (DropWhileState s a) a
forall s a. Step s a
Stop

    step' State StreamK m a
gst (DropWhileNext s
st) =  do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a))
-> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a b. (a -> b) -> a -> b
$ DropWhileState s a -> Step (DropWhileState s a) a
forall s a. s -> Step s a
Skip (a -> s -> DropWhileState s a
forall s a. a -> s -> DropWhileState s a
DropWhileYield a
x s
s)
            Skip s
s    -> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a))
-> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a b. (a -> b) -> a -> b
$ DropWhileState s a -> Step (DropWhileState s a) a
forall s a. s -> Step s a
Skip (s -> DropWhileState s a
forall s a. s -> DropWhileState s a
DropWhileNext s
s)
            Step s a
Stop      -> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (DropWhileState s a) a
forall s a. Step s a
Stop

    step' State StreamK m a
_ (DropWhileYield a
x s
st) = Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a))
-> Step (DropWhileState s a) a -> m (Step (DropWhileState s a) a)
forall a b. (a -> b) -> a -> b
$ a -> DropWhileState s a -> Step (DropWhileState s a) a
forall s a. a -> s -> Step s a
Yield a
x (s -> DropWhileState s a
forall s a. s -> DropWhileState s a
DropWhileNext s
st)

-- | Drop elements in the stream as long as the predicate succeeds and then
-- take the rest of the stream.
--
{-# INLINE dropWhile #-}
dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
-- dropWhile p = scanMaybe (FL.droppingWhile p)
dropWhile :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
dropWhile a -> Bool
f = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
dropWhileM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

-- | Drop @n@ elements at the end of the stream.
--
-- O(n) space, where n is the number elements dropped.
--
-- /Unimplemented/
{-# INLINE dropLast #-}
dropLast :: -- Monad m =>
    Int -> Stream m a -> Stream m a
dropLast :: forall (m :: * -> *) a. Int -> Stream m a -> Stream m a
dropLast = Int -> Stream m a -> Stream m a
forall a. HasCallStack => a
undefined -- fromStreamD $ D.dropLast n $ toStreamD m

-- | Drop all consecutive elements at the end of the stream for which the
-- predicate is true.
--
-- O(n) space, where n is the number elements dropped.
--
-- /Unimplemented/
{-# INLINE dropWhileLast #-}
dropWhileLast :: -- Monad m =>
    (a -> Bool) -> Stream m a -> Stream m a
dropWhileLast :: forall a (m :: * -> *). (a -> Bool) -> Stream m a -> Stream m a
dropWhileLast = (a -> Bool) -> Stream m a -> Stream m a
forall a. HasCallStack => a
undefined -- fromStreamD $ D.dropWhileLast n $ toStreamD m

-- | Like 'dropWhile' and 'dropWhileLast' combined.
--
-- O(n) space, where n is the number elements dropped from the end.
--
-- /Unimplemented/
{-# INLINE dropWhileAround #-}
dropWhileAround :: -- Monad m =>
    (a -> Bool) -> Stream m a -> Stream m a
dropWhileAround :: forall a (m :: * -> *). (a -> Bool) -> Stream m a -> Stream m a
dropWhileAround = (a -> Bool) -> Stream m a -> Stream m a
forall a. HasCallStack => a
undefined -- fromStreamD $ D.dropWhileAround n $ toStreamD m

------------------------------------------------------------------------------
-- Inserting Elements
------------------------------------------------------------------------------

-- | @insertBy cmp elem stream@ inserts @elem@ before the first element in
-- @stream@ that is less than @elem@ when compared using @cmp@.
--
-- >>> insertBy cmp x = Stream.mergeBy cmp (Stream.fromPure x)
--
-- >>> input = Stream.fromList [1,3,5]
-- >>> Stream.fold Fold.toList $ Stream.insertBy compare 2 input
-- [1,2,3,5]
--
{-# INLINE_NORMAL insertBy #-}
insertBy :: Monad m => (a -> a -> Ordering) -> a -> Stream m a -> Stream m a
insertBy :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> Ordering) -> a -> Stream m a -> Stream m a
insertBy a -> a -> Ordering
cmp a
a (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a
 -> (s, Bool, Maybe a) -> m (Step (s, Bool, Maybe a) a))
-> (s, Bool, Maybe a) -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a
-> (s, Bool, Maybe a) -> m (Step (s, Bool, Maybe a) a)
step' (s
state, Bool
False, Maybe a
forall a. Maybe a
Nothing)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a
-> (s, Bool, Maybe a) -> m (Step (s, Bool, Maybe a) a)
step' State StreamK m a
gst (s
st, Bool
False, Maybe a
_) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> case a -> a -> Ordering
cmp a
a a
x of
                Ordering
GT -> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a))
-> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Bool, Maybe a) -> Step (s, Bool, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
x (s
s, Bool
False, Maybe a
forall a. Maybe a
Nothing)
                Ordering
_  -> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a))
-> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Bool, Maybe a) -> Step (s, Bool, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
a (s
s, Bool
True, a -> Maybe a
forall a. a -> Maybe a
Just a
x)
            Skip s
s -> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a))
-> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Bool, Maybe a) -> Step (s, Bool, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, Bool
False, Maybe a
forall a. Maybe a
Nothing)
            Step s a
Stop   -> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a))
-> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Bool, Maybe a) -> Step (s, Bool, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
a (s
st, Bool
True, Maybe a
forall a. Maybe a
Nothing)

    step' State StreamK m a
_ (s
_, Bool
True, Maybe a
Nothing) = Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Bool, Maybe a) a
forall s a. Step s a
Stop

    step' State StreamK m a
gst (s
st, Bool
True, Just a
prev) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a))
-> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Bool, Maybe a) -> Step (s, Bool, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
prev (s
s, Bool
True, a -> Maybe a
forall a. a -> Maybe a
Just a
x)
            Skip s
s    -> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a))
-> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Bool, Maybe a) -> Step (s, Bool, Maybe a) a
forall s a. s -> Step s a
Skip (s
s, Bool
True, a -> Maybe a
forall a. a -> Maybe a
Just a
prev)
            Step s a
Stop      -> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a))
-> Step (s, Bool, Maybe a) a -> m (Step (s, Bool, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Bool, Maybe a) -> Step (s, Bool, Maybe a) a
forall s a. a -> s -> Step s a
Yield a
prev (s
st, Bool
True, Maybe a
forall a. Maybe a
Nothing)

data LoopState x s = FirstYield s
                   | InterspersingYield s
                   | YieldAndCarry x s

-- | Effectful variant of 'intersperse'. Insert an effect and its output
-- between successive elements of a stream. It does nothing if stream has less
-- than two elements.
--
-- Definition:
--
-- >>> intersperseM x = Stream.interleaveSepBy (Stream.repeatM x)
--
{-# INLINE_NORMAL intersperseM #-}
intersperseM :: Monad m => m a -> Stream m a -> Stream m a
intersperseM :: forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
intersperseM m a
m (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a -> LoopState a s -> m (Step (LoopState a s) a))
-> LoopState a s -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> LoopState a s -> m (Step (LoopState a s) a)
step' (s -> LoopState a s
forall x s. s -> LoopState x s
FirstYield s
state)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> LoopState a s -> m (Step (LoopState a s) a)
step' State StreamK m a
gst (FirstYield s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (LoopState a s) a -> m (Step (LoopState a s) a))
-> Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a b. (a -> b) -> a -> b
$
            case Step s a
r of
                Yield a
x s
s -> LoopState a s -> Step (LoopState a s) a
forall s a. s -> Step s a
Skip (a -> s -> LoopState a s
forall x s. x -> s -> LoopState x s
YieldAndCarry a
x s
s)
                Skip s
s -> LoopState a s -> Step (LoopState a s) a
forall s a. s -> Step s a
Skip (s -> LoopState a s
forall x s. s -> LoopState x s
FirstYield s
s)
                Step s a
Stop -> Step (LoopState a s) a
forall s a. Step s a
Stop

    step' State StreamK m a
gst (InterspersingYield s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> do
                a
a <- m a
m
                Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (LoopState a s) a -> m (Step (LoopState a s) a))
-> Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a b. (a -> b) -> a -> b
$ a -> LoopState a s -> Step (LoopState a s) a
forall s a. a -> s -> Step s a
Yield a
a (a -> s -> LoopState a s
forall x s. x -> s -> LoopState x s
YieldAndCarry a
x s
s)
            Skip s
s -> Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (LoopState a s) a -> m (Step (LoopState a s) a))
-> Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a b. (a -> b) -> a -> b
$ LoopState a s -> Step (LoopState a s) a
forall s a. s -> Step s a
Skip (LoopState a s -> Step (LoopState a s) a)
-> LoopState a s -> Step (LoopState a s) a
forall a b. (a -> b) -> a -> b
$ s -> LoopState a s
forall x s. s -> LoopState x s
InterspersingYield s
s
            Step s a
Stop -> Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (LoopState a s) a
forall s a. Step s a
Stop

    step' State StreamK m a
_ (YieldAndCarry a
x s
st) = Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (LoopState a s) a -> m (Step (LoopState a s) a))
-> Step (LoopState a s) a -> m (Step (LoopState a s) a)
forall a b. (a -> b) -> a -> b
$ a -> LoopState a s -> Step (LoopState a s) a
forall s a. a -> s -> Step s a
Yield a
x (s -> LoopState a s
forall x s. s -> LoopState x s
InterspersingYield s
st)

-- | Insert a pure value between successive elements of a stream. It does
-- nothing if stream has less than two elements.
--
-- Definition:
--
-- >>> intersperse x = Stream.intersperseM (return x)
-- >>> intersperse x = Stream.unfoldEachSepBy x Unfold.identity
-- >>> intersperse x = Stream.unfoldEachSepBySeq x Unfold.identity
-- >>> intersperse x = Stream.interleaveSepBy (Stream.repeat x)
--
-- Example:
--
-- >>> f x y = Stream.toList $ Stream.intersperse x $ Stream.fromList y
-- >>> f ',' "abc"
-- "a,b,c"
-- >>> f ',' "a"
-- "a"
--
{-# INLINE intersperse #-}
intersperse :: Monad m => a -> Stream m a -> Stream m a
intersperse :: forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
intersperse a
a = m a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
intersperseM (a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a)

-- | Perform a side effect between two successive elements of a stream. It does
-- nothing if the stream has less than two elements.
--
-- >>> f x y = Stream.fold Fold.drain $ Stream.trace putChar $ Stream.intersperseM_ x $ Stream.fromList y
-- >>> f (putChar '.') "abc"
-- a.b.c
-- >>> f (putChar '.') "a"
-- a
--
-- /Pre-release/
{-# INLINE_NORMAL intersperseM_ #-}
intersperseM_ :: Monad m => m b -> Stream m a -> Stream m a
intersperseM_ :: forall (m :: * -> *) b a.
Monad m =>
m b -> Stream m a -> Stream m a
intersperseM_ m b
m (Stream State StreamK m a -> s -> m (Step s a)
step1 s
state1) = (State StreamK m a
 -> Either (m (), s) s -> m (Step (Either (m (), s) s) a))
-> Either (m (), s) s -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a
-> Either (m (), s) s -> m (Step (Either (m (), s) s) a)
step ((m (), s) -> Either (m (), s) s
forall a b. a -> Either a b
Left (() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (), s
state1))
  where
    {-# INLINE_LATE step #-}
    step :: State StreamK m a
-> Either (m (), s) s -> m (Step (Either (m (), s) s) a)
step State StreamK m a
gst (Left (m ()
eff, s
st)) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> m ()
eff m ()
-> m (Step (Either (m (), s) s) a)
-> m (Step (Either (m (), s) s) a)
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Either (m (), s) s -> Step (Either (m (), s) s) a
forall s a. a -> s -> Step s a
Yield a
x (s -> Either (m (), s) s
forall a b. b -> Either a b
Right s
s))
            Skip s
s -> Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a))
-> Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a)
forall a b. (a -> b) -> a -> b
$ Either (m (), s) s -> Step (Either (m (), s) s) a
forall s a. s -> Step s a
Skip ((m (), s) -> Either (m (), s) s
forall a b. a -> Either a b
Left (m ()
eff, s
s))
            Step s a
Stop -> Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Either (m (), s) s) a
forall s a. Step s a
Stop

    step State StreamK m a
_ (Right s
st) = Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a))
-> Step (Either (m (), s) s) a -> m (Step (Either (m (), s) s) a)
forall a b. (a -> b) -> a -> b
$ Either (m (), s) s -> Step (Either (m (), s) s) a
forall s a. s -> Step s a
Skip (Either (m (), s) s -> Step (Either (m (), s) s) a)
-> Either (m (), s) s -> Step (Either (m (), s) s) a
forall a b. (a -> b) -> a -> b
$ (m (), s) -> Either (m (), s) s
forall a b. a -> Either a b
Left (m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void m b
m, s
st)

-- | Intersperse a monadic action into the input stream after every @n@
-- elements.
--
-- Definition:
--
-- >> intersperseEveryM n x = Stream.interleaveEverySepBy n (Stream.repeatM x)
--
-- Idioms:
--
-- >>> intersperseM = Stream.intersperseEveryM 1
-- >>> intersperse x = Stream.intersperseEveryM 1 (return x)
--
-- Usage:
--
-- >> input = Stream.fromList "hello"
-- >> Stream.toList $ Stream.intersperseEveryM 2 (return ',') input
-- "he,ll,o"
--
-- /Unimplemented/
{-# INLINE intersperseEveryM #-}
intersperseEveryM :: -- Monad m =>
    Int -> m a -> Stream m a -> Stream m a
intersperseEveryM :: forall (m :: * -> *) a. Int -> m a -> Stream m a -> Stream m a
intersperseEveryM Int
_n m a
_f Stream m a
_xs = Stream m a
forall a. HasCallStack => a
undefined

data SuffixState s a
    = SuffixElem s
    | SuffixSuffix s
    | SuffixYield a (SuffixState s a)

-- | Insert an effect and its output after every element of a stream.
--
-- Definition:
--
-- >>> intersperseEndByM x = Stream.interleaveEndBy (Stream.repeatM x)
--
-- Usage:
--
-- >>> f x y = Stream.toList $ Stream.intersperseEndByM (pure x) $ Stream.fromList y
-- >>> f ',' "abc"
-- "a,b,c,"
-- >>> f ',' "a"
-- "a,"
--
-- /Pre-release/
{-# INLINE_NORMAL intersperseEndByM #-}
intersperseEndByM, intersperseMSuffix :: forall m a. Monad m =>
    m a -> Stream m a -> Stream m a
intersperseEndByM :: forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
intersperseEndByM m a
action (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m a
 -> SuffixState s a -> m (Step (SuffixState s a) a))
-> SuffixState s a -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a
-> SuffixState s a -> m (Step (SuffixState s a) a)
step' (s -> SuffixState s a
forall s a. s -> SuffixState s a
SuffixElem s
state)
    where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a
-> SuffixState s a -> m (Step (SuffixState s a) a)
step' State StreamK m a
gst (SuffixElem s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        Step (SuffixState s a) a -> m (Step (SuffixState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixState s a) a -> m (Step (SuffixState s a) a))
-> Step (SuffixState s a) a -> m (Step (SuffixState s a) a)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
            Yield a
x s
s -> SuffixState s a -> Step (SuffixState s a) a
forall s a. s -> Step s a
Skip (a -> SuffixState s a -> SuffixState s a
forall s a. a -> SuffixState s a -> SuffixState s a
SuffixYield a
x (s -> SuffixState s a
forall s a. s -> SuffixState s a
SuffixSuffix s
s))
            Skip s
s -> SuffixState s a -> Step (SuffixState s a) a
forall s a. s -> Step s a
Skip (s -> SuffixState s a
forall s a. s -> SuffixState s a
SuffixElem s
s)
            Step s a
Stop -> Step (SuffixState s a) a
forall s a. Step s a
Stop

    step' State StreamK m a
_ (SuffixSuffix s
st) = do
        m a
action m a
-> (a -> m (Step (SuffixState s a) a))
-> m (Step (SuffixState s a) a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
r -> Step (SuffixState s a) a -> m (Step (SuffixState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixState s a) a -> m (Step (SuffixState s a) a))
-> Step (SuffixState s a) a -> m (Step (SuffixState s a) a)
forall a b. (a -> b) -> a -> b
$ SuffixState s a -> Step (SuffixState s a) a
forall s a. s -> Step s a
Skip (a -> SuffixState s a -> SuffixState s a
forall s a. a -> SuffixState s a -> SuffixState s a
SuffixYield a
r (s -> SuffixState s a
forall s a. s -> SuffixState s a
SuffixElem s
st))

    step' State StreamK m a
_ (SuffixYield a
x SuffixState s a
next) = Step (SuffixState s a) a -> m (Step (SuffixState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixState s a) a -> m (Step (SuffixState s a) a))
-> Step (SuffixState s a) a -> m (Step (SuffixState s a) a)
forall a b. (a -> b) -> a -> b
$ a -> SuffixState s a -> Step (SuffixState s a) a
forall s a. a -> s -> Step s a
Yield a
x SuffixState s a
next

RENAME(intersperseMSuffix,intersperseEndByM)

-- | Insert an effect after every element of a stream.
--
-- Example:
--
-- >>> f x y = Stream.fold Fold.drain $ Stream.trace putChar $ Stream.intersperseEndByM_ x $ Stream.fromList y
-- >>> f (putChar '.') "abc"
-- a.b.c.
-- >>> f (putChar '.') "a"
-- a.
--
-- /Pre-release/
--
{-# INLINE_NORMAL intersperseEndByM_ #-}
intersperseEndByM_, intersperseMSuffix_ :: Monad m => m b -> Stream m a -> Stream m a
intersperseEndByM_ :: forall (m :: * -> *) b a.
Monad m =>
m b -> Stream m a -> Stream m a
intersperseEndByM_ m b
m (Stream State StreamK m a -> s -> m (Step s a)
step1 s
state1) = (State StreamK m a -> Either s s -> m (Step (Either s s) a))
-> Either s s -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> Either s s -> m (Step (Either s s) a)
step (s -> Either s s
forall a b. a -> Either a b
Left s
state1)
  where
    {-# INLINE_LATE step #-}
    step :: State StreamK m a -> Either s s -> m (Step (Either s s) a)
step State StreamK m a
gst (Left s
st) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 State StreamK m a
gst s
st
        case Step s a
r of
            Yield a
x s
s -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ a -> Either s s -> Step (Either s s) a
forall s a. a -> s -> Step s a
Yield a
x (s -> Either s s
forall a b. b -> Either a b
Right s
s)
            Skip s
s -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (Either s s -> Step (Either s s) a)
-> Either s s -> Step (Either s s) a
forall a b. (a -> b) -> a -> b
$ s -> Either s s
forall a b. a -> Either a b
Left s
s
            Step s a
Stop -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Either s s) a
forall s a. Step s a
Stop

    step State StreamK m a
_ (Right s
st) = m b
m m b -> m (Step (Either s s) a) -> m (Step (Either s s) a)
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. a -> Either a b
Left s
st))

RENAME(intersperseMSuffix_,intersperseEndByM_)

data SuffixSpanState s a
    = SuffixSpanElem s Int
    | SuffixSpanSuffix s
    | SuffixSpanYield a (SuffixSpanState s a)
    | SuffixSpanLast
    | SuffixSpanStop

-- | Like 'intersperseEndByM' but intersperses an effectful action into the
-- input stream after every @n@ elements and also after the last element.
--
-- Example:
--
-- >>> input = Stream.fromList "hello"
-- >>> Stream.toList $ Stream.intersperseEndByEveryM 2 (return ',') input
-- "he,ll,o,"
-- >>> f n x y = Stream.toList $ Stream.intersperseEndByEveryM n (pure x) $ Stream.fromList y
-- >>> f 2 ',' "abcdef"
-- "ab,cd,ef,"
-- >>> f 2 ',' "abcdefg"
-- "ab,cd,ef,g,"
-- >>> f 2 ',' "a"
-- "a,"
--
-- /Pre-release/
--
{-# INLINE_NORMAL intersperseEndByEveryM #-}
intersperseEndByEveryM, intersperseMSuffixWith :: forall m a. Monad m
    => Int -> m a -> Stream m a -> Stream m a
intersperseEndByEveryM :: forall (m :: * -> *) a.
Monad m =>
Int -> m a -> Stream m a -> Stream m a
intersperseEndByEveryM Int
n m a
action (Stream State StreamK m a -> s -> m (Step s a)
step s
state) =
    (State StreamK m a
 -> SuffixSpanState s a -> m (Step (SuffixSpanState s a) a))
-> SuffixSpanState s a -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a
-> SuffixSpanState s a -> m (Step (SuffixSpanState s a) a)
step' (s -> Int -> SuffixSpanState s a
forall s a. s -> Int -> SuffixSpanState s a
SuffixSpanElem s
state Int
n)
    where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a
-> SuffixSpanState s a -> m (Step (SuffixSpanState s a) a)
step' State StreamK m a
gst (SuffixSpanElem s
st Int
i) | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
        Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a))
-> Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
            Yield a
x s
s -> SuffixSpanState s a -> Step (SuffixSpanState s a) a
forall s a. s -> Step s a
Skip (a -> SuffixSpanState s a -> SuffixSpanState s a
forall s a. a -> SuffixSpanState s a -> SuffixSpanState s a
SuffixSpanYield a
x (s -> Int -> SuffixSpanState s a
forall s a. s -> Int -> SuffixSpanState s a
SuffixSpanElem s
s (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)))
            Skip s
s -> SuffixSpanState s a -> Step (SuffixSpanState s a) a
forall s a. s -> Step s a
Skip (s -> Int -> SuffixSpanState s a
forall s a. s -> Int -> SuffixSpanState s a
SuffixSpanElem s
s Int
i)
            Step s a
Stop -> if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n then Step (SuffixSpanState s a) a
forall s a. Step s a
Stop else SuffixSpanState s a -> Step (SuffixSpanState s a) a
forall s a. s -> Step s a
Skip SuffixSpanState s a
forall s a. SuffixSpanState s a
SuffixSpanLast
    step' State StreamK m a
_ (SuffixSpanElem s
st Int
_) = Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a))
-> Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a b. (a -> b) -> a -> b
$ SuffixSpanState s a -> Step (SuffixSpanState s a) a
forall s a. s -> Step s a
Skip (s -> SuffixSpanState s a
forall s a. s -> SuffixSpanState s a
SuffixSpanSuffix s
st)

    step' State StreamK m a
_ (SuffixSpanSuffix s
st) = do
        m a
action m a
-> (a -> m (Step (SuffixSpanState s a) a))
-> m (Step (SuffixSpanState s a) a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
r -> Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a))
-> Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a b. (a -> b) -> a -> b
$ SuffixSpanState s a -> Step (SuffixSpanState s a) a
forall s a. s -> Step s a
Skip (a -> SuffixSpanState s a -> SuffixSpanState s a
forall s a. a -> SuffixSpanState s a -> SuffixSpanState s a
SuffixSpanYield a
r (s -> Int -> SuffixSpanState s a
forall s a. s -> Int -> SuffixSpanState s a
SuffixSpanElem s
st Int
n))

    step' State StreamK m a
_ SuffixSpanState s a
SuffixSpanLast = do
        m a
action m a
-> (a -> m (Step (SuffixSpanState s a) a))
-> m (Step (SuffixSpanState s a) a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
r -> Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a))
-> Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a b. (a -> b) -> a -> b
$ SuffixSpanState s a -> Step (SuffixSpanState s a) a
forall s a. s -> Step s a
Skip (a -> SuffixSpanState s a -> SuffixSpanState s a
forall s a. a -> SuffixSpanState s a -> SuffixSpanState s a
SuffixSpanYield a
r SuffixSpanState s a
forall s a. SuffixSpanState s a
SuffixSpanStop)

    step' State StreamK m a
_ (SuffixSpanYield a
x SuffixSpanState s a
next) = Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a))
-> Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a b. (a -> b) -> a -> b
$ a -> SuffixSpanState s a -> Step (SuffixSpanState s a) a
forall s a. a -> s -> Step s a
Yield a
x SuffixSpanState s a
next

    step' State StreamK m a
_ SuffixSpanState s a
SuffixSpanStop = Step (SuffixSpanState s a) a -> m (Step (SuffixSpanState s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (SuffixSpanState s a) a
forall s a. Step s a
Stop

RENAME(intersperseMSuffixWith,intersperseEndByEveryM)

-- | Insert a side effect before every element of a stream.
--
-- Definition:
--
-- >>> intersperseBeginByM_ = Stream.trace_
-- >>> intersperseBeginByM_ m = Stream.mapM (\x -> void m >> return x)
--
-- Usage:
--
-- >>> f x y = Stream.fold Fold.drain $ Stream.trace putChar $ Stream.intersperseBeginByM_ x $ Stream.fromList y
-- >>> f (putChar '.') "abc"
-- .a.b.c
--
-- Same as 'trace_'.
--
-- /Pre-release/
--
{-# INLINE intersperseBeginByM_ #-}
intersperseBeginByM_, intersperseMPrefix_ :: Monad m =>
    m b -> Stream m a -> Stream m a
intersperseBeginByM_ :: forall (m :: * -> *) b a.
Monad m =>
m b -> Stream m a -> Stream m a
intersperseBeginByM_ m b
m = (a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM (\a
x -> m b -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void m b
m m () -> m a -> m a
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x)

RENAME(intersperseMPrefix_,intersperseBeginByM_)

------------------------------------------------------------------------------
-- Inserting Time
------------------------------------------------------------------------------

-- XXX This should be in Prelude, should we export this as a helper function?

-- | Block the current thread for specified number of seconds.
{-# INLINE sleep #-}
sleep :: MonadIO m => Double -> m ()
sleep :: forall (m :: * -> *). MonadIO m => Double -> m ()
sleep Double
n = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> IO ()
threadDelay (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> Int) -> Double -> Int
forall a b. (a -> b) -> a -> b
$ Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000000

-- | Introduce a delay of specified seconds between elements of the stream.
--
-- Definition:
--
-- >>> sleep n = liftIO $ threadDelay $ round $ n * 1000000
-- >>> delay = Stream.intersperseM_ . sleep
--
-- Example:
--
-- >>> input = Stream.enumerateFromTo 1 3
-- >>> Stream.fold (Fold.drainMapM print) $ Stream.delay 1 input
-- 1
-- 2
-- 3
--
{-# INLINE delay #-}
delay :: MonadIO m => Double -> Stream m a -> Stream m a
delay :: forall (m :: * -> *) a.
MonadIO m =>
Double -> Stream m a -> Stream m a
delay = m () -> Stream m a -> Stream m a
forall (m :: * -> *) b a.
Monad m =>
m b -> Stream m a -> Stream m a
intersperseM_ (m () -> Stream m a -> Stream m a)
-> (Double -> m ()) -> Double -> Stream m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> m ()
forall (m :: * -> *). MonadIO m => Double -> m ()
sleep

-- | Introduce a delay of specified seconds after consuming an element of a
-- stream.
--
-- Definition:
--
-- >>> sleep n = liftIO $ threadDelay $ round $ n * 1000000
-- >>> delayPost = Stream.intersperseEndByM_ . sleep
--
-- Example:
--
-- >>> input = Stream.enumerateFromTo 1 3
-- >>> Stream.fold (Fold.drainMapM print) $ Stream.delayPost 1 input
-- 1
-- 2
-- 3
--
-- /Pre-release/
--
{-# INLINE delayPost #-}
delayPost :: MonadIO m => Double -> Stream m a -> Stream m a
delayPost :: forall (m :: * -> *) a.
MonadIO m =>
Double -> Stream m a -> Stream m a
delayPost Double
n = m () -> Stream m a -> Stream m a
forall (m :: * -> *) b a.
Monad m =>
m b -> Stream m a -> Stream m a
intersperseMSuffix_ (m () -> Stream m a -> Stream m a)
-> m () -> Stream m a -> Stream m a
forall a b. (a -> b) -> a -> b
$ IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> IO ()
threadDelay (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double -> Int) -> Double -> Int
forall a b. (a -> b) -> a -> b
$ Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000000

-- | Introduce a delay of specified seconds before consuming an element of a
-- stream.
--
-- Definition:
--
-- >>> sleep n = liftIO $ threadDelay $ round $ n * 1000000
-- >>> delayPre = Stream.intersperseBeginByM_ . sleep
--
-- Example:
--
-- >>> input = Stream.enumerateFromTo 1 3
-- >>> Stream.fold (Fold.drainMapM print) $ Stream.delayPre 1 input
-- 1
-- 2
-- 3
--
-- /Pre-release/
--
{-# INLINE delayPre #-}
delayPre :: MonadIO m => Double -> Stream m a -> Stream m a
delayPre :: forall (m :: * -> *) a.
MonadIO m =>
Double -> Stream m a -> Stream m a
delayPre = m () -> Stream m a -> Stream m a
forall (m :: * -> *) b a.
Monad m =>
m b -> Stream m a -> Stream m a
intersperseMPrefix_(m () -> Stream m a -> Stream m a)
-> (Double -> m ()) -> Double -> Stream m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> m ()
forall (m :: * -> *). MonadIO m => Double -> m ()
sleep

------------------------------------------------------------------------------
-- Reordering
------------------------------------------------------------------------------

-- | Returns the elements of the stream in reverse order.  The stream must be
-- finite. Note that this necessarily buffers the entire stream in memory.
--
-- Definition:
--
-- >>> reverse m = Stream.concatEffect $ Stream.fold Fold.toListRev m >>= return . Stream.fromList
--
{-# INLINE_NORMAL reverse #-}
reverse :: Monad m => Stream m a -> Stream m a
reverse :: forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
reverse Stream m a
m = m (Stream m a) -> Stream m a
forall (m :: * -> *) a. Monad m => m (Stream m a) -> Stream m a
concatEffect (m (Stream m a) -> Stream m a) -> m (Stream m a) -> Stream m a
forall a b. (a -> b) -> a -> b
$ Fold m a [a] -> Stream m a -> m [a]
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
fold Fold m a [a]
forall (m :: * -> *) a. Monad m => Fold m a [a]
FL.toListRev Stream m a
m m [a] -> ([a] -> Stream m a) -> m (Stream m a)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> [a] -> Stream m a
forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
fromList
{-
reverse m = Stream step Nothing
    where
    {-# INLINE_LATE step #-}
    step _ Nothing = do
        xs <- foldl' (flip (:)) [] m
        return $ Skip (Just xs)
    step _ (Just (x:xs)) = return $ Yield x (Just xs)
    step _ (Just []) = return Stop
-}

-- | Like 'reverse' but several times faster, requires an 'Unbox' instance.
--
-- /O(n) space/
--
-- /Pre-release/
{-# INLINE reverseUnbox #-}
reverseUnbox :: (MonadIO m, Unbox a) => Stream m a -> Stream m a
reverseUnbox :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> Stream m a
reverseUnbox =
    Stream m (Array a) -> Stream m a
forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Stream m (Array a) -> Stream m a
A.concatRev -- unfoldMany A.readerRev
        (Stream m (Array a) -> Stream m a)
-> (Stream m a -> Stream m (Array a)) -> Stream m a -> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StreamK m (Array a) -> Stream m (Array a)
forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
fromStreamK
        (StreamK m (Array a) -> Stream m (Array a))
-> (Stream m a -> StreamK m (Array a))
-> Stream m a
-> Stream m (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StreamK m (Array a) -> StreamK m (Array a)
forall (m :: * -> *) a. StreamK m a -> StreamK m a
K.reverse
        (StreamK m (Array a) -> StreamK m (Array a))
-> (Stream m a -> StreamK m (Array a))
-> Stream m a
-> StreamK m (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stream m (Array a) -> StreamK m (Array a)
forall (m :: * -> *) a. Monad m => Stream m a -> StreamK m a
toStreamK
        (Stream m (Array a) -> StreamK m (Array a))
-> (Stream m a -> Stream m (Array a))
-> Stream m a
-> StreamK m (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Stream m a -> Stream m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
A.chunksOf Int
defaultChunkSize

-- | Buffer until the next element in sequence arrives. The function argument
-- determines the difference in sequence numbers. This could be useful in
-- implementing sequenced streams, for example, TCP reassembly.
--
-- /Unimplemented/
--
{-# INLINE reassembleBy #-}
reassembleBy
    :: -- Monad m =>
       Fold m a b
    -> (a -> a -> Int)
    -> Stream m a
    -> Stream m b
reassembleBy :: forall (m :: * -> *) a b.
Fold m a b -> (a -> a -> Int) -> Stream m a -> Stream m b
reassembleBy = Fold m a b -> (a -> a -> Int) -> Stream m a -> Stream m b
forall a. HasCallStack => a
undefined

------------------------------------------------------------------------------
-- Position Indexing
------------------------------------------------------------------------------

-- Adapted from the vector package

-- |
-- >>> f = Scanl.mkScanl (\(i, _) x -> (i + 1, x)) (-1,undefined)
-- >>> indexed = Stream.postscanl f
-- >>> indexed = Stream.zipWith (,) (Stream.enumerateFrom 0)
-- >>> indexedR n = fmap (\(i, a) -> (n - i, a)) . indexed
--
-- Pair each element in a stream with its index, starting from index 0.
--
-- >>> Stream.fold Fold.toList $ Stream.indexed $ Stream.fromList "hello"
-- [(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]
--
{-# INLINE_NORMAL indexed #-}
indexed :: Monad m => Stream m a -> Stream m (Int, a)
-- indexed = scanMaybe FL.indexing
indexed :: forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
indexed (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m (Int, a)
 -> (s, Int) -> m (Step (s, Int) (Int, a)))
-> (s, Int) -> Stream m (Int, a)
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m (Int, a) -> (s, Int) -> m (Step (s, Int) (Int, a))
forall {b} {m :: * -> *} {a}.
Num b =>
State StreamK m a -> (s, b) -> m (Step (s, b) (b, a))
step' (s
state, Int
0)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, b) -> m (Step (s, b) (b, a))
step' State StreamK m a
gst (s
st, b
i) = b
i b -> m (Step (s, b) (b, a)) -> m (Step (s, b) (b, a))
forall a b. a -> b -> b
`seq` do
         Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
         case Step s a
r of
             Yield a
x s
s -> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, b) (b, a) -> m (Step (s, b) (b, a)))
-> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a b. (a -> b) -> a -> b
$ (b, a) -> (s, b) -> Step (s, b) (b, a)
forall s a. a -> s -> Step s a
Yield (b
i, a
x) (s
s, b
ib -> b -> b
forall a. Num a => a -> a -> a
+b
1)
             Skip    s
s -> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, b) (b, a) -> m (Step (s, b) (b, a)))
-> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a b. (a -> b) -> a -> b
$ (s, b) -> Step (s, b) (b, a)
forall s a. s -> Step s a
Skip (s
s, b
i)
             Step s a
Stop      -> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, b) (b, a)
forall s a. Step s a
Stop

-- Adapted from the vector package

-- |
-- >>> f n = Scanl.mkScanl (\(i, _) x -> (i - 1, x)) (n + 1,undefined)
-- >>> indexedR n = Stream.postscanl (f n)
--
-- >>> s n = Stream.enumerateFromThen n (n - 1)
-- >>> indexedR n = Stream.zipWith (,) (s n)
--
-- Pair each element in a stream with its index, starting from the
-- given index @n@ and counting down.
--
-- >>> Stream.fold Fold.toList $ Stream.indexedR 10 $ Stream.fromList "hello"
-- [(10,'h'),(9,'e'),(8,'l'),(7,'l'),(6,'o')]
--
{-# INLINE_NORMAL indexedR #-}
indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
-- indexedR n = scanMaybe (FL.indexingRev n)
indexedR :: forall (m :: * -> *) a.
Monad m =>
Int -> Stream m a -> Stream m (Int, a)
indexedR Int
m (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m (Int, a)
 -> (s, Int) -> m (Step (s, Int) (Int, a)))
-> (s, Int) -> Stream m (Int, a)
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m (Int, a) -> (s, Int) -> m (Step (s, Int) (Int, a))
forall {b} {m :: * -> *} {a}.
Num b =>
State StreamK m a -> (s, b) -> m (Step (s, b) (b, a))
step' (s
state, Int
m)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, b) -> m (Step (s, b) (b, a))
step' State StreamK m a
gst (s
st, b
i) = b
i b -> m (Step (s, b) (b, a)) -> m (Step (s, b) (b, a))
forall a b. a -> b -> b
`seq` do
         Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
         case Step s a
r of
             Yield a
x s
s -> let i' :: b
i' = b
i b -> b -> b
forall a. Num a => a -> a -> a
- b
1
                          in Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, b) (b, a) -> m (Step (s, b) (b, a)))
-> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a b. (a -> b) -> a -> b
$ (b, a) -> (s, b) -> Step (s, b) (b, a)
forall s a. a -> s -> Step s a
Yield (b
i, a
x) (s
s, b
i')
             Skip    s
s -> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, b) (b, a) -> m (Step (s, b) (b, a)))
-> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a b. (a -> b) -> a -> b
$ (s, b) -> Step (s, b) (b, a)
forall s a. s -> Step s a
Skip (s
s, b
i)
             Step s a
Stop      -> Step (s, b) (b, a) -> m (Step (s, b) (b, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, b) (b, a)
forall s a. Step s a
Stop

-------------------------------------------------------------------------------
-- Time Indexing
-------------------------------------------------------------------------------

-- Note: The timestamp stream must be the second stream in the zip so that the
-- timestamp is generated after generating the stream element and not before.
-- If we do not do that then the following example will generate the same
-- timestamp for first two elements:
--
-- Stream.fold Fold.toList $ Stream.timestamped $ Stream.delay $ Stream.enumerateFromTo 1 3

-- | Pair each element in a stream with an absolute timestamp, using a clock of
-- specified granularity.  The timestamp is generated just before the element
-- is consumed.
--
-- >>> Stream.fold Fold.toList $ Stream.timestampWith 0.01 $ Stream.delay 1 $ Stream.enumerateFromTo 1 3
-- [(AbsTime (TimeSpec {sec = ..., nsec = ...}),1),(AbsTime (TimeSpec {sec = ..., nsec = ...}),2),(AbsTime (TimeSpec {sec = ..., nsec = ...}),3)]
--
-- /Pre-release/
--
{-# INLINE timestampWith #-}
timestampWith :: (MonadIO m)
    => Double -> Stream m a -> Stream m (AbsTime, a)
timestampWith :: forall (m :: * -> *) a.
MonadIO m =>
Double -> Stream m a -> Stream m (AbsTime, a)
timestampWith Double
g Stream m a
stream = (a -> AbsTime -> (AbsTime, a))
-> Stream m a -> Stream m AbsTime -> Stream m (AbsTime, a)
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith ((AbsTime -> a -> (AbsTime, a)) -> a -> AbsTime -> (AbsTime, a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (,)) Stream m a
stream (Double -> Stream m AbsTime
forall (m :: * -> *). MonadIO m => Double -> Stream m AbsTime
absTimesWith Double
g)

-- TBD: check performance vs a custom implementation without using zipWith.
--
-- /Pre-release/
--
{-# INLINE timestamped #-}
timestamped :: (MonadIO m)
    => Stream m a -> Stream m (AbsTime, a)
timestamped :: forall (m :: * -> *) a.
MonadIO m =>
Stream m a -> Stream m (AbsTime, a)
timestamped = Double -> Stream m a -> Stream m (AbsTime, a)
forall (m :: * -> *) a.
MonadIO m =>
Double -> Stream m a -> Stream m (AbsTime, a)
timestampWith Double
0.01

-- | Pair each element in a stream with relative times starting from 0, using a
-- clock with the specified granularity. The time is measured just before the
-- element is consumed.
--
-- >>> Stream.fold Fold.toList $ Stream.timeIndexWith 0.01 $ Stream.delay 1 $ Stream.enumerateFromTo 1 3
-- [(RelTime64 (NanoSecond64 ...),1),(RelTime64 (NanoSecond64 ...),2),(RelTime64 (NanoSecond64 ...),3)]
--
-- /Pre-release/
--
{-# INLINE timeIndexWith #-}
timeIndexWith :: (MonadIO m)
    => Double -> Stream m a -> Stream m (RelTime64, a)
timeIndexWith :: forall (m :: * -> *) a.
MonadIO m =>
Double -> Stream m a -> Stream m (RelTime64, a)
timeIndexWith Double
g Stream m a
stream = (a -> RelTime64 -> (RelTime64, a))
-> Stream m a -> Stream m RelTime64 -> Stream m (RelTime64, a)
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith ((RelTime64 -> a -> (RelTime64, a))
-> a -> RelTime64 -> (RelTime64, a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (,)) Stream m a
stream (Double -> Stream m RelTime64
forall (m :: * -> *). MonadIO m => Double -> Stream m RelTime64
relTimesWith Double
g)

-- | Pair each element in a stream with relative times starting from 0, using a
-- 10 ms granularity clock. The time is measured just before the element is
-- consumed.
--
-- >>> Stream.fold Fold.toList $ Stream.timeIndexed $ Stream.delay 1 $ Stream.enumerateFromTo 1 3
-- [(RelTime64 (NanoSecond64 ...),1),(RelTime64 (NanoSecond64 ...),2),(RelTime64 (NanoSecond64 ...),3)]
--
-- /Pre-release/
--
{-# INLINE timeIndexed #-}
timeIndexed :: (MonadIO m)
    => Stream m a -> Stream m (RelTime64, a)
timeIndexed :: forall (m :: * -> *) a.
MonadIO m =>
Stream m a -> Stream m (RelTime64, a)
timeIndexed = Double -> Stream m a -> Stream m (RelTime64, a)
forall (m :: * -> *) a.
MonadIO m =>
Double -> Stream m a -> Stream m (RelTime64, a)
timeIndexWith Double
0.01

------------------------------------------------------------------------------
-- Searching
------------------------------------------------------------------------------

-- | Find all the indices where the element in the stream satisfies the given
-- predicate.
--
-- >>> findIndices p = Stream.postscanlMaybe (Scanl.findIndices p)
--
{-# INLINE_NORMAL findIndices #-}
findIndices :: Monad m => (a -> Bool) -> Stream m a -> Stream m Int
findIndices :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m Int
findIndices a -> Bool
p (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = (State StreamK m Int -> (s, Int) -> m (Step (s, Int) Int))
-> (s, Int) -> Stream m Int
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m Int -> (s, Int) -> m (Step (s, Int) Int)
forall {b} {m :: * -> *} {a}.
Num b =>
State StreamK m a -> (s, b) -> m (Step (s, b) b)
step' (s
state, Int
0)
  where
    {-# INLINE_LATE step' #-}
    step' :: State StreamK m a -> (s, b) -> m (Step (s, b) b)
step' State StreamK m a
gst (s
st, b
i) = b
i b -> m (Step (s, b) b) -> m (Step (s, b) b)
forall a b. a -> b -> b
`seq` do
      Step s a
r <- State StreamK m a -> s -> m (Step s a)
step (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
      Step (s, b) b -> m (Step (s, b) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, b) b -> m (Step (s, b) b))
-> Step (s, b) b -> m (Step (s, b) b)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
          Yield a
x s
s -> if a -> Bool
p a
x then b -> (s, b) -> Step (s, b) b
forall s a. a -> s -> Step s a
Yield b
i (s
s, b
ib -> b -> b
forall a. Num a => a -> a -> a
+b
1) else (s, b) -> Step (s, b) b
forall s a. s -> Step s a
Skip (s
s, b
ib -> b -> b
forall a. Num a => a -> a -> a
+b
1)
          Skip s
s -> (s, b) -> Step (s, b) b
forall s a. s -> Step s a
Skip (s
s, b
i)
          Step s a
Stop   -> Step (s, b) b
forall s a. Step s a
Stop

-- | Find all the indices where the value of the element in the stream is equal
-- to the given value.
--
-- >>> elemIndices a = Stream.findIndices (== a)
--
{-# INLINE elemIndices #-}
elemIndices :: (Monad m, Eq a) => a -> Stream m a -> Stream m Int
elemIndices :: forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> Stream m Int
elemIndices a
a = (a -> Bool) -> Stream m a -> Stream m Int
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m Int
findIndices (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a)

------------------------------------------------------------------------------
-- Rolling map
------------------------------------------------------------------------------

data RollingMapState s a = RollingMapGo s a

-- | Like 'rollingMap' but with an effectful map function.
--
-- /Pre-release/
--
{-# INLINE rollingMapM #-}
rollingMapM :: Monad m => (Maybe a -> a -> m b) -> Stream m a -> Stream m b
-- rollingMapM f = scanMaybe (FL.slide2 $ Window.rollingMapM f)
rollingMapM :: forall (m :: * -> *) a b.
Monad m =>
(Maybe a -> a -> m b) -> Stream m a -> Stream m b
rollingMapM Maybe a -> a -> m b
f (Stream State StreamK m a -> s -> m (Step s a)
step1 s
state1) = (State StreamK m b
 -> RollingMapState s (Maybe a)
 -> m (Step (RollingMapState s (Maybe a)) b))
-> RollingMapState s (Maybe a) -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b
-> RollingMapState s (Maybe a)
-> m (Step (RollingMapState s (Maybe a)) b)
forall {m :: * -> *} {a}.
State StreamK m a
-> RollingMapState s (Maybe a)
-> m (Step (RollingMapState s (Maybe a)) b)
step (s -> Maybe a -> RollingMapState s (Maybe a)
forall s a. s -> a -> RollingMapState s a
RollingMapGo s
state1 Maybe a
forall a. Maybe a
Nothing)

    where

    step :: State StreamK m a
-> RollingMapState s (Maybe a)
-> m (Step (RollingMapState s (Maybe a)) b)
step State StreamK m a
gst (RollingMapGo s
s1 Maybe a
curr) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
s1
        case Step s a
r of
            Yield a
x s
s -> do
                !b
res <- Maybe a -> a -> m b
f Maybe a
curr a
x
                Step (RollingMapState s (Maybe a)) b
-> m (Step (RollingMapState s (Maybe a)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (RollingMapState s (Maybe a)) b
 -> m (Step (RollingMapState s (Maybe a)) b))
-> Step (RollingMapState s (Maybe a)) b
-> m (Step (RollingMapState s (Maybe a)) b)
forall a b. (a -> b) -> a -> b
$ b
-> RollingMapState s (Maybe a)
-> Step (RollingMapState s (Maybe a)) b
forall s a. a -> s -> Step s a
Yield b
res (RollingMapState s (Maybe a)
 -> Step (RollingMapState s (Maybe a)) b)
-> RollingMapState s (Maybe a)
-> Step (RollingMapState s (Maybe a)) b
forall a b. (a -> b) -> a -> b
$ s -> Maybe a -> RollingMapState s (Maybe a)
forall s a. s -> a -> RollingMapState s a
RollingMapGo s
s (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
            Skip s
s -> Step (RollingMapState s (Maybe a)) b
-> m (Step (RollingMapState s (Maybe a)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (RollingMapState s (Maybe a)) b
 -> m (Step (RollingMapState s (Maybe a)) b))
-> Step (RollingMapState s (Maybe a)) b
-> m (Step (RollingMapState s (Maybe a)) b)
forall a b. (a -> b) -> a -> b
$ RollingMapState s (Maybe a) -> Step (RollingMapState s (Maybe a)) b
forall s a. s -> Step s a
Skip (RollingMapState s (Maybe a)
 -> Step (RollingMapState s (Maybe a)) b)
-> RollingMapState s (Maybe a)
-> Step (RollingMapState s (Maybe a)) b
forall a b. (a -> b) -> a -> b
$ s -> Maybe a -> RollingMapState s (Maybe a)
forall s a. s -> a -> RollingMapState s a
RollingMapGo s
s Maybe a
curr
            Step s a
Stop   -> Step (RollingMapState s (Maybe a)) b
-> m (Step (RollingMapState s (Maybe a)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (RollingMapState s (Maybe a)) b
forall s a. Step s a
Stop

-- rollingMap is a special case of an incremental sliding fold. It can be
-- written as:
--
-- > fld f = slidingWindow 1 (Scanl.mkScanl (\_ (x,y) -> f y x)
-- > rollingMap f = Stream.postscan (fld f) undefined

-- | Apply a function on every two successive elements of a stream. The first
-- argument of the map function is the previous element and the second argument
-- is the current element. When the current element is the first element, the
-- previous element is 'Nothing'.
--
-- /Pre-release/
--
{-# INLINE rollingMap #-}
rollingMap :: Monad m => (Maybe a -> a -> b) -> Stream m a -> Stream m b
-- rollingMap f = scanMaybe (FL.slide2 $ Window.rollingMap f)
rollingMap :: forall (m :: * -> *) a b.
Monad m =>
(Maybe a -> a -> b) -> Stream m a -> Stream m b
rollingMap Maybe a -> a -> b
f = (Maybe a -> a -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(Maybe a -> a -> m b) -> Stream m a -> Stream m b
rollingMapM (\Maybe a
x a
y -> b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> b -> m b
forall a b. (a -> b) -> a -> b
$ Maybe a -> a -> b
f Maybe a
x a
y)

-- | Like 'rollingMap' but requires at least two elements in the stream,
-- returns an empty stream otherwise.
--
-- This is the stream equivalent of the list idiom @zipWith f xs (tail xs)@.
--
-- /Pre-release/
--
{-# INLINE rollingMap2 #-}
rollingMap2 :: Monad m => (a -> a -> b) -> Stream m a -> Stream m b
rollingMap2 :: forall (m :: * -> *) a b.
Monad m =>
(a -> a -> b) -> Stream m a -> Stream m b
rollingMap2 a -> a -> b
f = Stream m (Maybe b) -> Stream m b
forall (m :: * -> *) a. Monad m => Stream m (Maybe a) -> Stream m a
catMaybes (Stream m (Maybe b) -> Stream m b)
-> (Stream m a -> Stream m (Maybe b)) -> Stream m a -> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> a -> Maybe b) -> Stream m a -> Stream m (Maybe b)
forall (m :: * -> *) a b.
Monad m =>
(Maybe a -> a -> b) -> Stream m a -> Stream m b
rollingMap Maybe a -> a -> Maybe b
g

    where

    g :: Maybe a -> a -> Maybe b
g Maybe a
Nothing a
_ = Maybe b
forall a. Maybe a
Nothing
    g (Just a
x) a
y = b -> Maybe b
forall a. a -> Maybe a
Just (a -> a -> b
f a
x a
y)

------------------------------------------------------------------------------
-- Maybe Streams
------------------------------------------------------------------------------

-- XXX Will this always fuse properly?

-- | Map a 'Maybe' returning function to a stream, filter out the 'Nothing'
-- elements, and return a stream of values extracted from 'Just'.
--
-- Equivalent to:
--
-- >>> mapMaybe f = Stream.catMaybes . fmap f
--
{-# INLINE_NORMAL mapMaybe #-}
mapMaybe :: Monad m => (a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe :: forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe a -> Maybe b
f = (Maybe b -> b) -> Stream m (Maybe b) -> Stream m b
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe b -> b
forall a. HasCallStack => Maybe a -> a
fromJust (Stream m (Maybe b) -> Stream m b)
-> (Stream m a -> Stream m (Maybe b)) -> Stream m a -> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe b -> Bool) -> Stream m (Maybe b) -> Stream m (Maybe b)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
filter Maybe b -> Bool
forall a. Maybe a -> Bool
isJust (Stream m (Maybe b) -> Stream m (Maybe b))
-> (Stream m a -> Stream m (Maybe b))
-> Stream m a
-> Stream m (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe b) -> Stream m a -> Stream m (Maybe b)
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
map a -> Maybe b
f

-- | Like 'mapMaybe' but maps a monadic function.
--
-- Equivalent to:
--
-- >>> mapMaybeM f = Stream.catMaybes . Stream.mapM f
--
-- >>> mapM f = Stream.mapMaybeM (\x -> Just <$> f x)
--
{-# INLINE_NORMAL mapMaybeM #-}
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
mapMaybeM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> Stream m a -> Stream m b
mapMaybeM a -> m (Maybe b)
f = (Maybe b -> b) -> Stream m (Maybe b) -> Stream m b
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe b -> b
forall a. HasCallStack => Maybe a -> a
fromJust (Stream m (Maybe b) -> Stream m b)
-> (Stream m a -> Stream m (Maybe b)) -> Stream m a -> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe b -> Bool) -> Stream m (Maybe b) -> Stream m (Maybe b)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
filter Maybe b -> Bool
forall a. Maybe a -> Bool
isJust (Stream m (Maybe b) -> Stream m (Maybe b))
-> (Stream m a -> Stream m (Maybe b))
-> Stream m a
-> Stream m (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m (Maybe b)) -> Stream m a -> Stream m (Maybe b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM a -> m (Maybe b)
f

-- | In a stream of 'Maybe's, discard 'Nothing's and unwrap 'Just's.
--
-- >>> catMaybes = Stream.mapMaybe id
-- >>> catMaybes = fmap fromJust . Stream.filter isJust
--
-- /Pre-release/
--
{-# INLINE catMaybes #-}
catMaybes :: Monad m => Stream m (Maybe a) -> Stream m a
-- catMaybes = fmap fromJust . filter isJust
catMaybes :: forall (m :: * -> *) a. Monad m => Stream m (Maybe a) -> Stream m a
catMaybes (Stream State StreamK m (Maybe a) -> s -> m (Step s (Maybe a))
step s
state) = (State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> s -> m (Step s a)
forall {m :: * -> *} {a}. State StreamK m a -> s -> m (Step s a)
step1 s
state

    where

    {-# INLINE_LATE step1 #-}
    step1 :: State StreamK m a -> s -> m (Step s a)
step1 State StreamK m a
gst s
st = do
        Step s (Maybe a)
r <- State StreamK m (Maybe a) -> s -> m (Step s (Maybe a))
step (State StreamK m a -> State StreamK m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s (Maybe a)
r of
            Yield Maybe a
x s
s -> do
                Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
                    (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ case Maybe a
x of
                        Just a
a -> a -> s -> Step s a
forall s a. a -> s -> Step s a
Yield a
a s
s
                        Maybe a
Nothing -> s -> Step s a
forall s a. s -> Step s a
Skip s
s
            Skip s
s -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s
            Step s (Maybe a)
Stop -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s a
forall s a. Step s a
Stop

-- | Use a filtering scan on a stream.
--
-- >>> postscanlMaybe f = Stream.catMaybes . Stream.postscanl f
--
{-# INLINE postscanlMaybe #-}
postscanlMaybe :: Monad m => Scanl m a (Maybe b) -> Stream m a -> Stream m b
postscanlMaybe :: forall (m :: * -> *) a b.
Monad m =>
Scanl m a (Maybe b) -> Stream m a -> Stream m b
postscanlMaybe Scanl m a (Maybe b)
f = Stream m (Maybe b) -> Stream m b
forall (m :: * -> *) a. Monad m => Stream m (Maybe a) -> Stream m a
catMaybes (Stream m (Maybe b) -> Stream m b)
-> (Stream m a -> Stream m (Maybe b)) -> Stream m a -> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scanl m a (Maybe b) -> Stream m a -> Stream m (Maybe b)
forall (m :: * -> *) a b.
Monad m =>
Scanl m a b -> Stream m a -> Stream m b
postscanl Scanl m a (Maybe b)
f

{-# DEPRECATED scanMaybe "Use postscanlMaybe instead" #-}
{-# INLINE scanMaybe #-}
scanMaybe :: Monad m => Fold m a (Maybe b) -> Stream m a -> Stream m b
scanMaybe :: forall (m :: * -> *) a b.
Monad m =>
Fold m a (Maybe b) -> Stream m a -> Stream m b
scanMaybe Fold m a (Maybe b)
f = Stream m (Maybe b) -> Stream m b
forall (m :: * -> *) a. Monad m => Stream m (Maybe a) -> Stream m a
catMaybes (Stream m (Maybe b) -> Stream m b)
-> (Stream m a -> Stream m (Maybe b)) -> Stream m a -> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fold m a (Maybe b) -> Stream m a -> Stream m (Maybe b)
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
postscan Fold m a (Maybe b)
f

------------------------------------------------------------------------------
-- Either streams
------------------------------------------------------------------------------

-- | Discard 'Right's and unwrap 'Left's in an 'Either' stream.
--
-- >>> catLefts = fmap (fromLeft undefined) . Stream.filter isLeft
--
-- /Pre-release/
--
{-# INLINE catLefts #-}
catLefts :: Monad m => Stream m (Either a b) -> Stream m a
catLefts :: forall (m :: * -> *) a b.
Monad m =>
Stream m (Either a b) -> Stream m a
catLefts = (Either a b -> a) -> Stream m (Either a b) -> Stream m a
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> Either a b -> a
forall a b. a -> Either a b -> a
fromLeft a
forall a. HasCallStack => a
undefined) (Stream m (Either a b) -> Stream m a)
-> (Stream m (Either a b) -> Stream m (Either a b))
-> Stream m (Either a b)
-> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either a b -> Bool)
-> Stream m (Either a b) -> Stream m (Either a b)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
filter Either a b -> Bool
forall a b. Either a b -> Bool
isLeft

-- | Discard 'Left's and unwrap 'Right's in an 'Either' stream.
--
-- >>> catRights = fmap (fromRight undefined) . Stream.filter isRight
--
-- /Pre-release/
--
{-# INLINE catRights #-}
catRights :: Monad m => Stream m (Either a b) -> Stream m b
catRights :: forall (m :: * -> *) a b.
Monad m =>
Stream m (Either a b) -> Stream m b
catRights = (Either a b -> b) -> Stream m (Either a b) -> Stream m b
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (b -> Either a b -> b
forall b a. b -> Either a b -> b
fromRight b
forall a. HasCallStack => a
undefined) (Stream m (Either a b) -> Stream m b)
-> (Stream m (Either a b) -> Stream m (Either a b))
-> Stream m (Either a b)
-> Stream m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either a b -> Bool)
-> Stream m (Either a b) -> Stream m (Either a b)
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
filter Either a b -> Bool
forall a b. Either a b -> Bool
isRight

-- | Remove the either wrapper and flatten both lefts and as well as rights in
-- the output stream.
--
-- >>> catEithers = fmap (either id id)
--
-- /Pre-release/
--
{-# INLINE catEithers #-}
catEithers :: Monad m => Stream m (Either a a) -> Stream m a
catEithers :: forall (m :: * -> *) a.
Monad m =>
Stream m (Either a a) -> Stream m a
catEithers = (Either a a -> a) -> Stream m (Either a a) -> Stream m a
forall a b. (a -> b) -> Stream m a -> Stream m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> (a -> a) -> Either a a -> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> a
forall a. a -> a
id a -> a
forall a. a -> a
id)

------------------------------------------------------------------------------
-- Splitting
------------------------------------------------------------------------------

-- Design note: If we use splitSepBy_ on an empty stream what should be the
-- result? Let's try the splitOn function in the "split" package:
--
-- > splitOn "a" ""
-- [""]
--
-- Round tripping the result through intercalate gives identity:
--
-- > intercalate "a" [""]
-- ""
--
-- Now let's try intercalate on empty list:
--
-- > intercalate "a" []
-- ""
--
-- Round tripping it with splitOn is not identity:
--
-- > splitOn "a" ""
-- [""]
--
-- Because intercalate flattens the two layers, both [] and [""] produce the
-- same result after intercalate. Therefore, inverse of intercalate is not
-- possible. We have to choose one of the two options for splitting an empty
-- stream.
--
-- Choosing empty stream as the result of splitting empty stream makes better
-- sense. This is different from the split package's choice. Splitting an empty
-- stream resulting into a non-empty stream seems a bit odd. Also, splitting
-- empty stream to empty stream is consistent with splitEndBy operation as
-- well.

{-# ANN type SplitSepBy Fuse #-}
data SplitSepBy s fs b a
    = SplitSepByInit s
    | SplitSepByInitFold0 s
    | SplitSepByInitFold1 s fs
    | SplitSepByCheck s a fs
    | SplitSepByNext s fs
    | SplitSepByYield b (SplitSepBy s fs b a)
    | SplitSepByDone

-- | Split on an infixed separator element, dropping the separator.  The
-- supplied 'Fold' is applied on the split segments.  Splits the stream on
-- separator elements determined by the supplied predicate, separator is
-- considered as infixed between two segments:
--
-- Definition:
--
--
-- Usage:
--
-- >>> splitOn p xs = Stream.fold Fold.toList $ Stream.splitSepBy_ p Fold.toList (Stream.fromList xs)
-- >>> splitOn (== '.') "a.b"
-- ["a","b"]
--
-- Splitting an empty stream results in an empty stream i.e. zero splits:
--
-- >>> splitOn (== '.') ""
-- []
--
-- If the stream does not contain the separator then it results in a single
-- split:
--
-- >>> splitOn (== '.') "abc"
-- ["abc"]
--
-- If one or both sides of the separator are missing then the empty segment on
-- that side is folded to the default output of the fold:
--
-- >>> splitOn (== '.') "."
-- ["",""]
--
-- >>> splitOn (== '.') ".a"
-- ["","a"]
--
-- >>> splitOn (== '.') "a."
-- ["a",""]
--
-- >>> splitOn (== '.') "a..b"
-- ["a","","b"]
--
-- 'splitSepBy_' is an inverse of 'unfoldEachSepBy':
--
-- > Stream.unfoldEachSepBy '.' Unfold.fromList . Stream.splitSepBy_ (== '.') Fold.toList === id
--
-- Assuming the input stream does not contain the separator:
--
-- > Stream.splitSepBy_ (== '.') Fold.toList . Stream.unfoldEachSepBy '.' Unfold.fromList === id
--
{-# INLINE splitSepBy_ #-}
splitSepBy_ :: Monad m => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
-- We can express the infix splitting in terms of optional suffix split
-- fold.  After applying a suffix split fold repeatedly if the last segment
-- ends with a suffix then we need to return the default output of the fold
-- after that to make it an infix split.
--
-- Alternately, we can also express it using an optional prefix split fold.
-- If the first segment starts with a prefix then we need to emit the
-- default output of the fold before that to make it an infix split, and
-- then apply prefix split fold repeatedly.
--
splitSepBy_ :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
splitSepBy_ a -> Bool
predicate (Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
_ s -> m b
final) (Stream State StreamK m a -> s -> m (Step s a)
step1 s
state1) =
    (State StreamK m b
 -> SplitSepBy s s b a -> m (Step (SplitSepBy s s b a) b))
-> SplitSepBy s s b a -> Stream m b
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m b
-> SplitSepBy s s b a -> m (Step (SplitSepBy s s b a) b)
forall {m :: * -> *} {a}.
State StreamK m a
-> SplitSepBy s s b a -> m (Step (SplitSepBy s s b a) b)
step (s -> SplitSepBy s s b a
forall s fs b a. s -> SplitSepBy s fs b a
SplitSepByInit s
state1)

    where

    -- Note: there is a question of whether we should initialize the fold
    -- before we run the stream or only after the stream yields an element. If
    -- we initialize it before then we may have to discard an effect if the
    -- stream does not yield anything. If we initialize it after then we may
    -- have to discard the stream element if the fold terminates without
    -- consuming anything. Though the state machine is simpler if we initialize
    -- the fold first. Also, in most common cases the fold is not effectful.
    -- On the other hand, in most cases the fold will not terminate without
    -- consuming anything. So both ways are similar.
    {-# INLINE_LATE step #-}
    step :: State StreamK m a
-> SplitSepBy s s b a -> m (Step (SplitSepBy s s b a) b)
step State StreamK m a
_ (SplitSepByInit s
st) = do
        Step s b
fres <- m (Step s b)
initial
        Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip
            (SplitSepBy s s b a -> Step (SplitSepBy s s b a) b)
-> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall a b. (a -> b) -> a -> b
$ case Step s b
fres of
                  FL.Done b
b -> b -> SplitSepBy s s b a -> SplitSepBy s s b a
forall s fs b a. b -> SplitSepBy s fs b a -> SplitSepBy s fs b a
SplitSepByYield b
b (s -> SplitSepBy s s b a
forall s fs b a. s -> SplitSepBy s fs b a
SplitSepByInit s
st)
                  FL.Partial s
fs -> s -> s -> SplitSepBy s s b a
forall s fs b a. s -> fs -> SplitSepBy s fs b a
SplitSepByInitFold1 s
st s
fs

    step State StreamK m a
_ (SplitSepByInitFold0 s
st) = do
        Step s b
fres <- m (Step s b)
initial
        Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
            (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip
            (SplitSepBy s s b a -> Step (SplitSepBy s s b a) b)
-> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall a b. (a -> b) -> a -> b
$ case Step s b
fres of
                  FL.Done b
b -> b -> SplitSepBy s s b a -> SplitSepBy s s b a
forall s fs b a. b -> SplitSepBy s fs b a -> SplitSepBy s fs b a
SplitSepByYield b
b (s -> SplitSepBy s s b a
forall s fs b a. s -> SplitSepBy s fs b a
SplitSepByInitFold0 s
st)
                  FL.Partial s
fs -> s -> s -> SplitSepBy s s b a
forall s fs b a. s -> fs -> SplitSepBy s fs b a
SplitSepByNext s
st s
fs

    step State StreamK m a
gst (SplitSepByInitFold1 s
st s
fs) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip (SplitSepBy s s b a -> Step (SplitSepBy s s b a) b)
-> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall a b. (a -> b) -> a -> b
$ s -> a -> s -> SplitSepBy s s b a
forall s fs b a. s -> a -> fs -> SplitSepBy s fs b a
SplitSepByCheck s
s a
x s
fs
            Skip s
s -> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip (s -> s -> SplitSepBy s s b a
forall s fs b a. s -> fs -> SplitSepBy s fs b a
SplitSepByInitFold1 s
s s
fs)
            Step s a
Stop -> s -> m b
final s
fs m b
-> m (Step (SplitSepBy s s b a) b)
-> m (Step (SplitSepBy s s b a) b)
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (SplitSepBy s s b a) b
forall s a. Step s a
Stop

    step State StreamK m a
_ (SplitSepByCheck s
st a
x s
fs) = do
        if a -> Bool
predicate a
x
        then do
            b
b <- s -> m b
final s
fs
            Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip (SplitSepBy s s b a -> Step (SplitSepBy s s b a) b)
-> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall a b. (a -> b) -> a -> b
$ b -> SplitSepBy s s b a -> SplitSepBy s s b a
forall s fs b a. b -> SplitSepBy s fs b a -> SplitSepBy s fs b a
SplitSepByYield b
b (s -> SplitSepBy s s b a
forall s fs b a. s -> SplitSepBy s fs b a
SplitSepByInitFold0 s
st)
        else do
            Step s b
fres <- s -> a -> m (Step s b)
fstep s
fs a
x
            Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
                (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip
                (SplitSepBy s s b a -> Step (SplitSepBy s s b a) b)
-> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall a b. (a -> b) -> a -> b
$ case Step s b
fres of
                      FL.Done b
b -> b -> SplitSepBy s s b a -> SplitSepBy s s b a
forall s fs b a. b -> SplitSepBy s fs b a -> SplitSepBy s fs b a
SplitSepByYield b
b (s -> SplitSepBy s s b a
forall s fs b a. s -> SplitSepBy s fs b a
SplitSepByInitFold0 s
st)
                      FL.Partial s
fs1 -> s -> s -> SplitSepBy s s b a
forall s fs b a. s -> fs -> SplitSepBy s fs b a
SplitSepByNext s
st s
fs1

    step State StreamK m a
gst (SplitSepByNext s
st s
fs) = do
        Step s a
r <- State StreamK m a -> s -> m (Step s a)
step1 (State StreamK m a -> State StreamK m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
        case Step s a
r of
            Yield a
x s
s -> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip (SplitSepBy s s b a -> Step (SplitSepBy s s b a) b)
-> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall a b. (a -> b) -> a -> b
$ s -> a -> s -> SplitSepBy s s b a
forall s fs b a. s -> a -> fs -> SplitSepBy s fs b a
SplitSepByCheck s
s a
x s
fs
            Skip s
s -> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip (s -> s -> SplitSepBy s s b a
forall s fs b a. s -> fs -> SplitSepBy s fs b a
SplitSepByNext s
s s
fs)
            Step s a
Stop -> do
                b
b <- s -> m b
final s
fs
                Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. s -> Step s a
Skip (SplitSepBy s s b a -> Step (SplitSepBy s s b a) b)
-> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall a b. (a -> b) -> a -> b
$ b -> SplitSepBy s s b a -> SplitSepBy s s b a
forall s fs b a. b -> SplitSepBy s fs b a -> SplitSepBy s fs b a
SplitSepByYield b
b SplitSepBy s s b a
forall s fs b a. SplitSepBy s fs b a
SplitSepByDone

    step State StreamK m a
_ (SplitSepByYield b
b SplitSepBy s s b a
next) = Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b))
-> Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a b. (a -> b) -> a -> b
$ b -> SplitSepBy s s b a -> Step (SplitSepBy s s b a) b
forall s a. a -> s -> Step s a
Yield b
b SplitSepBy s s b a
next
    step State StreamK m a
_ SplitSepBy s s b a
SplitSepByDone = Step (SplitSepBy s s b a) b -> m (Step (SplitSepBy s s b a) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (SplitSepBy s s b a) b
forall s a. Step s a
Stop

{-# DEPRECATED splitOn "Please use splitSepBy_ instead. Note the difference in behavior on splitting empty stream." #-}
{-# INLINE splitOn #-}
splitOn :: Monad m => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
splitOn :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
splitOn a -> Bool
predicate Fold m a b
f =
    Fold m a b -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> Stream m b
foldManyPost ((a -> Bool) -> Fold m a b -> Fold m a b
forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Fold m a b
FL.takeEndBy_ a -> Bool
predicate Fold m a b
f)