Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
This module builds on module Control.Foldl, adding stateful transducers and grouping operations.
- type Transduction a b = forall x. Fold b x -> Fold a x
- type Transduction' a b r = forall x. Fold b x -> Fold a (r, x)
- data Transducer i o r = forall x . Transducer (x -> i -> (x, [o], [[o]])) x (x -> (r, [o]))
- type TransductionM m a b = forall x. Monad m => FoldM m b x -> FoldM m a x
- type TransductionM' m a b r = forall x. FoldM m b x -> FoldM m a (r, x)
- data TransducerM m i o r = forall x . TransducerM (x -> i -> m (x, [o], [[o]])) (m x) (x -> m (r, [o]))
- transduce :: Transducer i o s -> Transduction i o
- transduce' :: Transducer i o s -> Transduction' i o s
- transduceM :: Monad m => TransducerM m i o s -> TransductionM m i o
- transduceM' :: Monad m => TransducerM m i o s -> TransductionM' m i o s
- groups :: Transducer a b s -> Transduction b c -> Transduction a c
- groups' :: Transducer a b s -> Fold u v -> Transduction' b c u -> Transduction' a c (s, v)
- groupsM :: Monad m => TransducerM m a b s -> TransductionM m b c -> TransductionM m a c
- groupsM' :: Monad m => TransducerM m a b s -> FoldM m u v -> TransductionM' m b c u -> TransductionM' m a c (s, v)
- folds :: Transducer a b s -> Fold b c -> Transduction a c
- folds' :: Transducer a b s -> Fold b c -> Transduction' a c s
- foldsM :: (Applicative m, Monad m) => TransducerM m a b s -> FoldM m b c -> TransductionM m a c
- foldsM' :: (Applicative m, Monad m) => TransducerM m a b s -> FoldM m b c -> TransductionM' m a c s
- take :: Int -> Transducer a a ()
- takeWhile :: (a -> Bool) -> Transducer a a ()
- drop :: Int -> Transducer a a ()
- dropWhile :: (a -> Bool) -> Transducer a a ()
- surround :: (Foldable p, Foldable s) => p a -> s a -> Transducer a a ()
- surroundIO :: (Foldable p, Foldable s, Functor m, MonadIO m) => m (p a) -> m (s a) -> TransducerM m a a ()
- chunksOf :: Int -> Transducer a a ()
- _generalize :: Monad m => Transducer i o s -> TransducerM m i o s
- _simplify :: TransducerM Identity i o s -> Transducer i o s
- foldify :: Transducer i o s -> Fold i s
- foldifyM :: Functor m => TransducerM m i o s -> FoldM m i s
- hoistTransducer :: Monad m => (forall a. m a -> n a) -> TransducerM m i o s -> TransducerM n i o s
- hoistFold :: Monad m => (forall a. m a -> n a) -> FoldM m i r -> FoldM n i r
- module Data.Functor.Extend
- module Control.Foldl
Transducer types
type Transduction a b = forall x. Fold b x -> Fold a x Source
A (possibly stateful) transformation on the inputs of a Fold
.
Functions constructed with combinators like premap
or handles
from
Control.Foldl also typecheck as a Transduction
.
type Transduction' a b r = forall x. Fold b x -> Fold a (r, x) Source
A more general from of Transduction
that adds new information to the
return value of the Fold
.
data Transducer i o r Source
A stateful process that transforms a stream of inputs into a stream of outputs, and may optionally demarcate groups in the stream of outputs.
Composed of a step function, an initial state, and a extraction function.
The step function returns a triplet of:
- The new internal state.
- Outputs that continue the last segment detected in the previous step.
A list of lists containing outputs for segments detected in the current step. If the list is empty, that means no splitting has taken place in the current step.
Transducer
s that do not perform grouping never return anything other than[]
here. In effect, they treat the whole stream as a single group.The extraction function returns the
Transducer
s own result value, as well as any pending outputs.
forall x . Transducer (x -> i -> (x, [o], [[o]])) x (x -> (r, [o])) |
Bifunctor (Transducer i) | |
Functor (Transducer i o) | |
Comonad (Transducer i o) | |
Extend (Transducer i o) |
type TransductionM m a b = forall x. Monad m => FoldM m b x -> FoldM m a x Source
Like Transduction
, but works on monadic Fold
s.
type TransductionM' m a b r = forall x. FoldM m b x -> FoldM m a (r, x) Source
Like Transduction'
, but works on monadic Fold
s.
data TransducerM m i o r Source
Like Transducer
, but monadic.
forall x . TransducerM (x -> i -> m (x, [o], [[o]])) (m x) (x -> m (r, [o])) |
(Functor m, Monad m) => Bifunctor (TransducerM m i) | |
Monad m => Functor (TransducerM m i o) | |
Monad m => Extend (TransducerM m i o) |
Applying transducers
transduce :: Transducer i o s -> Transduction i o Source
Apply a Transducer
to a Fold
, discarding the return value of the
Transducer
.
>>>
L.fold (transduce (Transducer (\_ i -> ((),[i],[])) () (\_ -> ('r',[]))) L.list) [1..7]
[1,2,3,4,5,6,7]
transduce' :: Transducer i o s -> Transduction' i o s Source
Generalized version of transduce
that preserves the return value of
the Transducer
.
>>>
L.fold (transduce' (Transducer (\_ i -> ((),[i],[])) () (\_ -> ('r',[]))) L.list) [1..7]
('r',[1,2,3,4,5,6,7])
transduceM :: Monad m => TransducerM m i o s -> TransductionM m i o Source
transduceM' :: Monad m => TransducerM m i o s -> TransductionM' m i o s Source
Like transduce'
, but works on monadic Fold
s.
Working with groups
groups :: Transducer a b s -> Transduction b c -> Transduction a c Source
Repeatedly applies a Transduction
to process each of the groups
demarcated by a Transducer
, returning a Fold
what works over the
undivided stream of inputs.
The return value of the Transducer
is discarded.
>>>
L.fold (groups (chunksOf 2) (transduce (surround "<" ">")) L.list) "aabbccdd"
"<aa><bb><cc><dd>"
:: Transducer a b s | |
-> Fold u v | auxiliary |
-> Transduction' b c u | repeatedly applied for processing each group |
-> Transduction' a c (s, v) |
Generalized version of groups
that preserves the return value of the
Transducer
.
A summary value for each group is also calculated. They are aggregated for
the whole stream, with the help of an auxiliary Fold
.
>>>
L.fold (groups' (chunksOf 2) L.list (\f -> transduce (surround "<" ">") (liftA2 (,) L.list f)) L.list) "aabbccdd"
(((),["<aa>","<bb>","<cc>","<dd>"]),"<aa><bb><cc><dd>")
groupsM :: Monad m => TransducerM m a b s -> TransductionM m b c -> TransductionM m a c Source
Monadic version of groups
.
groupsM' :: Monad m => TransducerM m a b s -> FoldM m u v -> TransductionM' m b c u -> TransductionM' m a c (s, v) Source
Monadic version of groups'
.
folds :: Transducer a b s -> Fold b c -> Transduction a c Source
Summarizes each of the groups demarcated by the Transducer
using a
Fold
.
The result value of the Transducer
is discarded.
>>>
L.fold (folds (chunksOf 3) L.sum L.list) [1..7]
[6,15,7]
folds' :: Transducer a b s -> Fold b c -> Transduction' a c s Source
Like folds
, but preserves the return value of the Transducer
.
>>>
L.fold (folds' (chunksOf 3) L.sum L.list) [1..7]
((),[6,15,7])
foldsM :: (Applicative m, Monad m) => TransducerM m a b s -> FoldM m b c -> TransductionM m a c Source
Monadic version of folds
.
foldsM' :: (Applicative m, Monad m) => TransducerM m a b s -> FoldM m b c -> TransductionM' m a c s Source
Monadic version of folds'
.
Transducers
take :: Int -> Transducer a a () Source
Pass the first n
inputs to the Fold
, and ignore the rest.
>>>
L.fold (transduce (take 2) L.list) [1..5]
[1,2]
>>>
L.fold (transduce (take 0) L.list) [1..5]
[]
takeWhile :: (a -> Bool) -> Transducer a a () Source
>>>
L.fold (transduce (takeWhile (<3)) L.list) [1..5]
[1,2]
drop :: Int -> Transducer a a () Source
Ignore the firs n
inputs, pass all subsequent inputs to the Fold
.
>>>
L.fold (transduce (drop 2) L.list) [1..5]
[3,4,5]
>>>
L.fold (transduce (drop 0) L.list) [1..5]
[1,2,3,4,5]
dropWhile :: (a -> Bool) -> Transducer a a () Source
>>>
L.fold (transduce (dropWhile (<3)) L.list) [1..5]
[3,4,5]
surround :: (Foldable p, Foldable s) => p a -> s a -> Transducer a a () Source
Adds a prefix and a suffix to the stream arriving into a Fold
.
>>>
L.fold (transduce (surround "prefix" "suffix") L.list) "middle"
"prefixmiddlesuffix"
surroundIO :: (Foldable p, Foldable s, Functor m, MonadIO m) => m (p a) -> m (s a) -> TransducerM m a a () Source
Splitters
chunksOf :: Int -> Transducer a a () Source
Splits a stream into chunks of fixed size.
>>>
L.fold (folds (chunksOf 2) L.list L.list) [1..7]
[[1,2],[3,4],[5,6],[7]]
>>>
L.fold (groups (chunksOf 2) (transduce (surround [] [0])) L.list) [1..7]
[1,2,0,3,4,0,5,6,0,7,0]
Transducer utilities
_generalize :: Monad m => Transducer i o s -> TransducerM m i o s Source
Generalize a Transducer
to a TransducerM
.
_simplify :: TransducerM Identity i o s -> Transducer i o s Source
Simplify a pure TransducerM
to a Transducer
.
foldify :: Transducer i o s -> Fold i s Source
Transforms a Transducer
into a Fold
by forgetting about the data sent
downstream.
hoistTransducer :: Monad m => (forall a. m a -> n a) -> TransducerM m i o s -> TransducerM n i o s Source
Changes the base monad used by a TransducerM
.
hoistFold :: Monad m => (forall a. m a -> n a) -> FoldM m i r -> FoldM n i r Source
Changes the base monad used by a FoldM
.
Re-exports
module Data.Functor.Extend
module Control.Foldl