streamly-0.8.1.1: Dataflow programming and declarative concurrency
Copyright(c) 2021 Composewell Technologies
LicenseBSD-3-Clause
Maintainerstreamly@composewell.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Streamly.Internal.Data.Array.Stream.Fold.Foreign

Description

Fold a stream of foreign arrays. Fold m a b in this module works on a stream of "Array a" and produces an output of type b.

Though Fold m a b in this module works on a stream of Array a it is different from Data.Fold m (Array a) b. While the latter works on arrays as a whole treating them as atomic elements, the folds in this module can work on the stream of arrays as if it is an element stream with all the arrays coalesced together. This module allows adapting the element stream folds in Data.Fold to correctly work on an array stream as if it is an element stream. For example:

>>> import qualified Streamly.Data.Fold as Fold
>>> import qualified Streamly.Internal.Data.Array.Stream.Foreign as ArrayStream
>>> import qualified Streamly.Internal.Data.Array.Stream.Fold.Foreign as ArrayFold
>>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (arraysOf)
>>> import qualified Streamly.Prelude as Stream
>>> ArrayStream.foldArr (ArrayFold.fromFold (Fold.take 7 Fold.toList)) $ Stream.arraysOf 5 $ Stream.fromList "hello world"
"hello w"
Synopsis

Documentation

newtype Fold m a b Source #

Array stream fold.

An array stream fold is basically an array stream Parser that does not fail. In case of array stream folds the count in Partial, Continue and Done is a count of elements that includes the leftover element count in the array that is currently being processed by the parser. If none of the elements is consumed by the parser the count is at least the whole array length. If the whole array is consumed by the parser then the count will be 0.

Pre-release

Constructors

Fold (Parser m (Array a) b) 

Instances

Instances details
MonadThrow m => Monad (Fold m a) Source #

Monad instance applies folds sequentially. Next fold can depend on the output of the previous fold. See concatMap.

(>>=) = flip concatMap
Instance details

Defined in Streamly.Internal.Data.Array.Stream.Fold.Foreign

Methods

(>>=) :: Fold m a a0 -> (a0 -> Fold m a b) -> Fold m a b #

(>>) :: Fold m a a0 -> Fold m a b -> Fold m a b #

return :: a0 -> Fold m a a0 #

Functor m => Functor (Fold m a) Source #

Maps a function over the result of fold.

Pre-release

Instance details

Defined in Streamly.Internal.Data.Array.Stream.Fold.Foreign

Methods

fmap :: (a0 -> b) -> Fold m a a0 -> Fold m a b #

(<$) :: a0 -> Fold m a b -> Fold m a a0 #

MonadThrow m => Applicative (Fold m a) Source #

Applicative form of serialWith. > (*) = serialWith id

Instance details

Defined in Streamly.Internal.Data.Array.Stream.Fold.Foreign

Methods

pure :: a0 -> Fold m a a0 #

(<*>) :: Fold m a (a0 -> b) -> Fold m a a0 -> Fold m a b #

liftA2 :: (a0 -> b -> c) -> Fold m a a0 -> Fold m a b -> Fold m a c #

(*>) :: Fold m a a0 -> Fold m a b -> Fold m a b #

(<*) :: Fold m a a0 -> Fold m a b -> Fold m a a0 #

Construction

fromFold :: forall m a b. (MonadIO m, Storable a) => Fold m a b -> Fold m a b Source #

Convert an element Fold into an array stream fold.

Pre-release

fromParser :: forall m a b. (MonadIO m, Storable a) => Parser m a b -> Fold m a b Source #

Convert an element Parser into an array stream fold. If the parser fails the fold would throw an exception.

Pre-release

fromArrayFold :: forall m a b. MonadIO m => Fold m (Array a) b -> Fold m a b Source #

Adapt an array stream fold.

Pre-release

Mapping

rmapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c Source #

Map a monadic function on the output of a fold.

Pre-release

Applicative

fromPure :: Monad m => b -> Fold m a b Source #

A fold that always yields a pure value without consuming any input.

Pre-release

fromEffect :: Monad m => m b -> Fold m a b Source #

A fold that always yields the result of an effectful action without consuming any input.

Pre-release

serialWith :: MonadThrow m => (a -> b -> c) -> Fold m x a -> Fold m x b -> Fold m x c Source #

Applies two folds sequentially on the input stream and combines their results using the supplied function.

Pre-release

Monad

concatMap :: MonadThrow m => (b -> Fold m a c) -> Fold m a b -> Fold m a c Source #

Applies a fold on the input stream, generates the next fold from the output of the previously applied fold and then applies that fold.

Pre-release

Transformation

take :: forall m a b. (Monad m, Storable a) => Int -> Fold m a b -> Fold m a b Source #