repa-stream-4.1.0.1: Stream functions not present in the vector library.

Safe HaskellSafe-Inferred
LanguageHaskell98

Data.Repa.Chain

Contents

Description

Synopsis

Chain Fusion

data Chain m s a Source

A chain is an abstract, stateful producer of elements. It is similar a stream as used in stream fusion, except that internal state is visible in its type. This allows the computation to be paused and resumed at a later point.

Constructors

Chain 

Fields

mchainSize :: Size

Expected size of the output.

mchainState :: s

Starting state.

mchainStep :: s -> m (Step s a)

Step the chain computation.

data Step s a Source

Result of a chain computation step.

Constructors

Yield !a !s

Yield an output value and a new seed.

Skip !s

Provide just a new seed.

Done !s

Signal that the computation has finished.

Instances

(Show s, Show a) => Show (Step s a) 

liftChain :: Monad m => Chain Identity s a -> Chain m s a Source

Lift a pure chain to a monadic chain.

resumeChain :: Monad m => s -> Chain m s a -> Chain m s a Source

Resume a chain computation from a previous state.

Weaves

weaveC Source

Arguments

:: Monad m 
=> (k -> Option aL -> Option aR -> m (Turn k aX))

Worker function.

-> k

Initial state.

-> Chain m sL aL

Left input chain.

-> Chain m sR aR

Right input chain.

-> Chain m (Weave sL aL sR aR k) aX

Result chain.

A weave is a generalized merge of two input chains.

The worker function takes the current state, values from the left and right input chains, and produces a Turn which describes any output at that point, as well as how the input chains should be advanced.

data Weave sL aL sR aR k Source

Internal state of a weave.

Instances

(Show sL, Show aL, Show sR, Show aR, Show k) => Show (Weave sL aL sR aR k) 

data Turn k a Source

What to do after considering two input elements.

Constructors

Give !a !k !Move

Give an element and a new state.

Next !k !Move

Move to the next input.

Finish !k !Move

Weave is finished for now.

Instances

(Show k, Show a) => Show (Turn k a) 

data Move Source

How to move the input chains after considering to input elements.

Instances

move :: k -> Move -> Weave s1 a1 s2 a2 k -> Weave s1 a1 s2 a2 k Source

Apply a Move instruction to a weave state.

Folding

foldsC Source

Arguments

:: Monad m 
=> (a -> b -> m b)

Worker function.

-> b

Initial state when folding rest of segments.

-> Option3 n Int b

Name, length and initial state for first segment.

-> Chain m sLen (n, Int)

Segment names and lengths.

-> Chain m sVal a

Input data to fold.

-> Chain m (Folds sLen sVal n a b) (n, b) 

Segmented fold over vectors of segment lengths and input values.

The total lengths of all segments need not match the length of the input elements vector. The returned Folds state can be inspected to determine whether all segments were completely folded, or the vector of segment lengths or elements was too short relative to the other.

data Folds sLens sVals n a b Source

Return state of a folds operation.

Constructors

Folds 

Fields

_stateLens :: !sLens

State of lengths chain.

_stateVals :: !sVals

State of values chain.

_nameSeg :: !(Option n)

If we're currently in a segment, then hold its name,

_lenSeg :: !Int

Length of current segment.

_valSeg :: !b

Accumulated value of current segment.

Instances

(Show sLens, Show sVals, Show n, Show b) => Show (Folds sLens sVals n a b) 

Scanning

scanMaybeC Source

Arguments

:: Monad m 
=> (k -> a -> m (k, Maybe b))

Worker function.

-> k

Initial state for scan.

-> Chain m s a

Input elements.

-> Chain m (s, k) b

Output elements and final state.

Perform a left-to-right scan through an input vector, maintaining a state value between each element. For each element of input we may or may not produce an element of output.

Grouping

groupsByC Source

Arguments

:: Monad m 
=> (a -> a -> m Bool)

Comparison function.

-> Maybe (a, Int)

Starting element and count.

-> Chain m s a

Input elements.

-> Chain m (s, Maybe (a, Int)) (a, Int) 

From a stream of values which has consecutive runs of idential values, produce a stream of the lengths of these runs.