pipes-extras-1.0.2: Extra utilities for pipes

Safe HaskellTrustworthy
LanguageHaskell2010

Pipes.Extras

Contents

Description

Unassorted utilities for pipes

Synopsis

ArrowChoice

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

Like arr from Arrow

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

Like left from ArrowChoice

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

Like right from ArrowChoice

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

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 b Source

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 b Source

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 r Source

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 r Source

Add a delay (in seconds) between each element

progress :: Pipe a a IO r Source

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 b Source

Strict fold of the elements of a Producer

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

Strict, monadic fold of the elements of a Producer

scan :: Monad m => Fold a b -> Pipe a b m r Source

Strict left scan

scanM :: Monad m => FoldM m a b -> Pipe a b m r Source

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 r Source

Strict left scan without explicit initial state

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

Strict, monadic left scan without explicit initial state

scan1i :: Monad m => (a -> a -> a) -> Pipe a a m r Source

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 r Source

Strict, monadic and endomorphic left scan without explicit initial state

Church encodings

toProxy :: Monad n => (forall m. Monad m => (a' -> (a -> m r) -> m r) -> (b -> (b' -> m r) -> m r) -> m r) -> Proxy a' a b' b n r Source

Build a Proxy from its church encoding

fromProxy :: Monad m => Proxy a' a b' b m r -> (a' -> (a -> m r) -> m r) -> (b -> (b' -> m r) -> m r) -> m r Source

Convert a Proxy to its church encoding