pipes-extras-1.0.0: Extra utilities for pipes

Safe HaskellTrustworthy

Pipes.Extras

Contents

Description

Unassorted utilities for pipes

Synopsis

ArrowChoice

arr :: Monad m => (a -> b) -> Pipe a b m rSource

Like arr from Arrow

left :: Monad m => Pipe a b m r -> Pipe (Either a x) (Either b x) m rSource

Like left from ArrowChoice

right :: Monad m => Pipe a b m r -> Pipe (Either x a) (Either x b) m rSource

Like right from ArrowChoice

(+++) :: Monad m => Pipe a b m r -> Pipe c d m r -> Pipe (Either a c) (Either b d) m rSource

Like (+++) from ArrowChoice

 pL +++ pR = left pL >-> right pR

Lenses

input :: Monad m => Setter (Proxy x' b y' y m r) (Proxy x' a y' y m r) a bSource

It helps to think in terms of the following simpler types:

 input :: Monad m => Setter' (Consumer a   m r) a
 input :: Monad m => Setter' (Pipe     a b m r) a

Note: This only works with lens and not lens-family-core

output :: Monad m => Setter (Proxy x' x y' a m r) (Proxy x' x y' b m r) a bSource

It helps to think in terms of the following simpler types:

 output :: Monad m => Setter' (Producer b m r) b
 output :: Monad m => Setter' (Pipe   a b m r) b

Note: This only works with lens and not lens-family-core

Fun

check :: Show a => Pipe a a IO rSource

Ask whether or not to let values pass through

>>> runEffect $ each [1..3] >-> check >-> Pipes.print
Allow <1> [Y/n]?
y<Enter>
1
Allow <2> [Y/n]?
no<Enter>
Allow <3> [Y/n]?
YES<Enter>
3

delay :: Double -> Pipe a a IO rSource

Add a delay (in seconds) between each element

progress :: Pipe a a IO rSource

Display a progress bar

This is very simple and only works if nothing else writes to the terminal

Try this:

>>> runEffect $ each [1..] >-> progress >-> delay 0.1 >-> Pipes.Prelude.drain

Foldl Compatibility

Note that you can already mix the pipes and foldl libraries without using pipes-extras just by using:

 import Control.Foldl (purely)
 import Pipes.Prelude (fold)

 purely fold :: Monad m => Fold a b -> Producer a m () -> m b

The following functions are for people who are too lazy to do even that.

fold :: Monad m => Fold a b -> Producer a m () -> m bSource

Strict fold of the elements of a Producer

foldM :: Monad m => FoldM m a b -> Producer a m () -> m bSource

Strict, monadic fold of the elements of a Producer

scan :: Monad m => Fold a b -> Pipe a b m rSource

Strict left scan

scanM :: Monad m => FoldM m a b -> Pipe a b m rSource

Strict, monadic left scan

Fold Variations

These are minor variations on left folds / scans

scan1 :: Monad m => (x -> a -> x) -> (a -> x) -> (x -> b) -> Pipe a b m rSource

Strict left scan without explicit initial state

scan1M :: Monad m => (x -> a -> m x) -> (a -> m x) -> (x -> m b) -> Pipe a b m rSource

Strict, monadic left scan without explicit initial state

scan1i :: Monad m => (a -> a -> a) -> Pipe a a m rSource

Strict, endomorphic left scan without explicit initial state.

 -- Compute exponential moving average
 ema :: (Monad m, Fractional a) => a -> Pipe a a m r
 ema α = scan1i (\last input -> last * α + input * (1 - α)

scan1iM :: Monad m => (a -> a -> m a) -> Pipe a a m rSource

Strict, monadic and endomorphic left scan without explicit initial state