pipes-group-1.0.3: Group streams into substreams

Safe Haskell | Safe
Language | Haskell2010

Pipes.Group

Contents

- Lenses
- Transformations
- Joiners
- Folds
- Re-exports

Description

Element-agnostic grouping utilities for `pipes`

See Pipes.Group.Tutorial for an extended tutorial

Synopsis

- groupsBy :: Monad m => (a' -> a' -> Bool) -> Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x)
- groupsBy' :: Monad m => (a' -> a' -> Bool) -> Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x)
- groups :: (Monad m, Eq a') => Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x)
- chunksOf :: Monad m => Int -> Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x)
- takes :: (Functor f, Monad m) => Int -> FreeT f m () -> FreeT f m ()
- takes' :: Monad m => Int -> FreeT (Producer a m) m x -> FreeT (Producer a m) m x
- drops :: Monad m => Int -> FreeT (Producer a m) m x -> FreeT (Producer a m) m x
- maps :: (Monad m, Functor g) => (forall r. f r -> g r) -> FreeT f m x -> FreeT g m x
- individually :: (Monad m, Functor g) => Setter (FreeT f m x) (FreeT g m x) (f (FreeT f m x)) (g (FreeT f m x))
- concats :: Monad m => FreeT (Producer a m) m x -> Producer a m x
- intercalates :: Monad m => Producer a m () -> FreeT (Producer a m) m x -> Producer a m x
- folds :: Monad m => (x -> a -> x) -> x -> (x -> b) -> FreeT (Producer a m) m r -> Producer b m r
- foldsM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> FreeT (Producer a m) m r -> Producer b m r
- module Control.Monad.Trans.Class
- module Control.Monad.Trans.Free
- module Pipes

# Lenses

groupsBy :: Monad m => (a' -> a' -> Bool) -> Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x) Source

`groupsBy` splits a `Producer` into a `FreeT` of `Producer`s grouped using the given equality predicate

    >>> import Lens.Family (view)
    >>> import Pipes (yield, each)
    >>> import Pipes.Prelude (toList)
    >>> (toList . intercalates (yield '|') . view (groupsBy (==))) (each "12233345")
    "1|22|333|4|5"

You can think of this as:

    groupsBy
        :: Monad m
        => (a -> a -> Bool) -> Lens' (Producer a m x) (FreeT (Producer a m) m x)

groupsBy' :: Monad m => (a' -> a' -> Bool) -> Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x) Source

`groupsBy'` splits a `Producer` into a `FreeT` of `Producer`s grouped using the given equality predicate

This differs from `groupsBy` by comparing successive elements for equality instead of comparing each element to the first member of the group

    >>> import Lens.Family (view)
    >>> import Pipes (yield, each)
    >>> import Pipes.Prelude (toList)
    >>> let cmp c1 c2 = succ c1 == c2
    >>> (toList . intercalates (yield '|') . view (groupsBy' cmp)) (each "12233345")
    "12|23|3|345"
    >>> (toList . intercalates (yield '|') . view (groupsBy  cmp)) (each "12233345")
    "122|3|3|34|5"

You can think of this as:

    groupsBy'
        :: Monad m
        => (a -> a -> Bool)
        -> Lens' (Producer a m x) (FreeT (Producer a m) m x)

groups :: (Monad m, Eq a') => Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x) Source

Like `groupsBy`, where the equality predicate is (`==`)

    >>> import Lens.Family (view)
    >>> import Pipes (yield, each)
    >>> import Pipes.Prelude (toList)
    >>> (toList . intercalates (yield '|') . view groups) (each "12233345")
    "1|22|333|4|5"

You can think of this as:

    groups
        :: (Monad m, Eq a) => Lens' (Producer a m x) (FreeT (Producer a m) m x)

chunksOf :: Monad m => Int -> Lens (Producer a' m x) (Producer a m x) (FreeT (Producer a' m) m x) (FreeT (Producer a m) m x) Source

`chunksOf` is an splits a `Producer` into a `FreeT` of `Producer`s of fixed length

    >>> import Lens.Family (view)
    >>> import Pipes (yield, each)
    >>> import Pipes.Prelude (toList)
    >>> (toList . intercalates (yield '|') . view (chunksOf 3)) (each "12233345")
    "122|333|45"

You can think of this as:

    chunksOf
        :: Monad m => Int -> Lens' (Producer a m x) (FreeT (Producer a m) m x)

# Transformations

takes :: (Functor f, Monad m) => Int -> FreeT f m () -> FreeT f m () Source

`(takes n)` only keeps the first `n` functor layers of a `FreeT`

    >>> import Lens.Family (view)
    >>> import Pipes (yield, each)
    >>> import Pipes.Prelude (toList)
    >>> (toList . intercalates (yield '|') . takes 3 . view groups) (each "12233345")
    "1|22|333"

You can think of this as:

    takes
        :: (Functor f, Monad m)
        => Int -> FreeT (Producer a m) m () -> FreeT (Producer a m) m ()

takes' :: Monad m => Int -> FreeT (Producer a m) m x -> FreeT (Producer a m) m x Source

`(takes' n)` only keeps the first `n` `Producer`s of a `FreeT`

`takes'` differs from `takes` by draining unused `Producer`s in order to preserve the return value. This makes it a suitable argument for `maps`.

drops :: Monad m => Int -> FreeT (Producer a m) m x -> FreeT (Producer a m) m x Source

`(drops n)` peels off the first `n` `Producer` layers of a `FreeT`

    >>> import Lens.Family (view)
    >>> import Pipes (yield, each)
    >>> import Pipes.Prelude (toList)
    >>> (toList . intercalates (yield '|') . drops 3 . view groups) (each "12233345")
    "4|5"

Use carefully: the peeling off is not free. This runs the first `n` layers, just discarding everything they produce.

maps :: (Monad m, Functor g) => (forall r. f r -> g r) -> FreeT f m x -> FreeT g m x Source

Transform each individual functor layer of a `FreeT`

You can think of this as:

    maps
        :: (forall r . Producer a m r -> Producer b m r)
        -> FreeT (Producer a m) m x -> FreeT (Producer b m) m x

This is just a synonym for `transFreeT`

individually :: (Monad m, Functor g) => Setter (FreeT f m x) (FreeT g m x) (f (FreeT f m x)) (g (FreeT f m x)) Source

Lens to transform each individual functor layer of a `FreeT`

    over individually = maps  -- ... with a less general type

# Joiners

concats :: Monad m => FreeT (Producer a m) m x -> Producer a m x Source

Join a `FreeT`-delimited stream of `Producer`s into a single `Producer`

intercalates :: Monad m => Producer a m () -> FreeT (Producer a m) m x -> Producer a m x Source

Join a `FreeT`-delimited stream of `Producer`s into a single `Producer` by intercalating a `Producer` in between them

# Folds

These folds are designed to be compatible with the `foldl` library. See the `purely` and `impurely` functions from that library for more details.

For example, to count the number of `Producer` layers in a `FreeT`, you can write:

    import Control.Applicative (pure)
    import qualified Control.Foldl as L
    import Pipes.Group
    import qualified Pipes.Prelude as P

    count :: Monad m => FreeT (Producer a m) m () -> m Int
    count = P.sum . L.purely folds (pure 1)

folds Source

Arguments

:: Monad m |
=> (x -> a -> x) | Step function
-> x | Initial accumulator
-> (x -> b) | Extraction function
-> FreeT (Producer a m) m r |
-> Producer b m r |

Fold each `Producer` of a `FreeT`

    purely folds
        :: Monad m => Fold a b -> FreeT (Producer a m) m r -> Producer b m r

foldsM Source

Arguments

:: Monad m |
=> (x -> a -> m x) | Step function
-> m x | Initial accumulator
-> (x -> m b) | Extraction function
-> FreeT (Producer a m) m r |
-> Producer b m r |

Fold each `Producer` of a `FreeT`, monadically

    impurely foldsM
        :: Monad m => FoldM a b -> FreeT (Producer a m) m r -> Producer b m r

# Re-exports

Control.Monad.Trans.Class re-exports `lift`.

Control.Monad.Trans.Free re-exports `FreeF` and `FreeT`

Pipes re-exports `Producer`, `yield`, and `next`.

module Control.Monad.Trans.Class

module Control.Monad.Trans.Free

module Pipes