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

Safe HaskellSafe
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

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) Source # 

Methods

showsPrec :: Int -> Step s a -> ShowS #

show :: Step s a -> String #

showList :: [Step s a] -> ShowS #

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

Lift a pure chain to a monadic chain.

resumeChain :: 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) Source # 

Methods

showsPrec :: Int -> Weave sL aL sR aR k -> ShowS #

show :: Weave sL aL sR aR k -> String #

showList :: [Weave sL aL sR aR k] -> ShowS #

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) Source # 

Methods

showsPrec :: Int -> Turn k a -> ShowS #

show :: Turn k a -> String #

showList :: [Turn k a] -> ShowS #

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

Instances

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

Methods

showsPrec :: Int -> Folds sLens sVals n a b -> ShowS #

show :: Folds sLens sVals n a b -> String #

showList :: [Folds sLens sVals n a b] -> ShowS #

Unfolding

unfoldsC Source #

Arguments

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

Instances

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

Methods

showsPrec :: Int -> StepUnfold s a -> ShowS #

show :: StepUnfold s a -> String #

showList :: [StepUnfold s a] -> ShowS #

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.