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

Safe HaskellSafe





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.




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.


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.


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

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.


weaveC Source


:: 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.


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

data Turn k a Source

What to do after considering two input elements.


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.


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

data Move Source

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


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

Apply a Move instruction to a weave state.


foldsC Source


:: 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.




_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.


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


unfoldsC Source


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

Worker function.

-> k

Initial state for the unfold.

-> Chain m s a

Input elements.

-> Chain m (s, k, Option a) b

Output elements.

Segmented unfold.

The worker function takes the current element from the input stream and current state.

If the worker returns Just x then that output element will be placed in the output stream, and it will be called again with the same input elemenent and next state.

If the worker returns Nothing then we advance to the next element of the input stream.

data StepUnfold s a Source


(Show s, Show a) => Show (StepUnfold s a) Source 


scanMaybeC Source


:: 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.


groupsByC Source


:: 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.