Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.Collate
Description
An Applicative Functor for extracting parts of a stream of values.
Basic usage involves building up a computation from calls to the sample
function, then running the computation against a Foldable
of inputs.
Operationally, this makes one pass over the input in order, extracting each
of the values specified by calls to sample
, then constructs the result
according to the Applicative structure.
Because this is written against ST
, we can run Collate
s purely, or
convert them to IO
as if by stToIO
, and run them with
actual I/O interspersed. This means a Collate
can be driven by a
streaming abstraction such as
conduit or
pipes.
Finally, although Collate
itself doesn't admit any reasonable Monad
implementation, it can be used with
free to describe multi-pass
algorithms over (repeatable) streams. To implement join
,
we'd potentially need to look over the whole stream of inputs to be able to
construct the inner Collate
and determine which inputs it needs to
inspect. So, we'd need to make multiple passes. If we extended the type to
support multiple passes and gave it a Monad
instance that implemented
(>>=
) by making two passes, then the Applicative
instance would also be
required to make two passes for (<*>
), because of the law that (
for any <*>
) =
ap
Monad
.
Synopsis
- newtype Collate c a = Collate {}
- newtype Collator m c = Collator {
- getCollator :: IntMap (c -> ST (PrimState m) ())
- sample :: Int -> (c -> a) -> Collate c a
- bulkSample :: Traversable t => t Int -> (c -> a) -> Collate c (t a)
- collate :: Foldable f => Collate c a -> f c -> a
- collateOf :: (forall s0. Traversing' (->) (Const (Sequenced () (StateT Int (ST s0)))) s c) -> Collate c a -> s -> a
- withCollator :: PrimMonad m => Collate c a -> (Collator m c -> m ()) -> m a
- feedCollatorOf :: forall m s c. PrimMonad m => Traversing' (->) (Const (Sequenced () (StateT Int (ST (PrimState m))))) s c -> Int -> Collator m c -> s -> m Int
- feedCollator :: forall m f c. (PrimMonad m, Foldable f) => Int -> Collator m c -> f c -> m Int
Types
Collate c a
is a strategy for extracting an a
from a sequence of c
s
in a single streaming pass over the input, even when lookups are specified
in arbitrary order.
Operationally, we build up a collection of mutable references, construct a
Collator
that describes how to fill all of them, construct an action that
will read the mutable references and return the ultimate result, iterate
over the input sequence to fill the mutable references, and finally run the
action to get the result.
An collection of "callbacks" for extracting things from a stream of values.
This is generated by Collate
, and holds many partially-applied
writeSTRef
s, so that once they've all been called, some larger value can
be extracted.
Constructors
Collator | |
Fields
|
Constructon
sample :: Int -> (c -> a) -> Collate c a Source #
Construct a primitive Collate
that strictly extracts the result of a
function from the input at the given index.
bulkSample :: Traversable t => t Int -> (c -> a) -> Collate c (t a) Source #
Construct a primitive Collate
that strictly extracts the result of a
function from many different indices.
Elimination
collateOf :: (forall s0. Traversing' (->) (Const (Sequenced () (StateT Int (ST s0)))) s c) -> Collate c a -> s -> a Source #
Run a Collate
on any Fold
.
The type signature looks complicated because we expand Fold
to avoid
incurring a dependency on lens
, but it's effectively just:
collateOf :: Fold s c -> Collate c a -> s -> a