machines-0.7.1: Networked stream transducers

Copyright(C) 2012 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityprovisional
PortabilityRank-2 Types
Safe HaskellNone
LanguageHaskell2010

Data.Machine.Source

Contents

Description

 
Synopsis

Sources

type Source b = forall k. Machine k b Source #

A Source never reads from its inputs.

type SourceT m b = forall k. MachineT m k b Source #

A SourceT never reads from its inputs, but may have monadic side-effects.

source :: Foldable f => f b -> Source b Source #

Generate a Source from any Foldable container.

This can be constructed from a plan with source :: Foldable f => f b -> Source b source = construct (traverse_ yield xs)

Examples:

>>> run $ source [1,2]
[1,2]

repeated :: o -> Source o Source #

Repeat the same value, over and over.

This can be constructed from a plan with repeated :: o -> Source o repeated = repeatedly . yield

Examples:

>>> run $ taking 5 <~ repeated 1
[1,1,1,1,1]

cycled :: Foldable f => f b -> Source b Source #

Loop through a Foldable container over and over.

This can be constructed from a plan with cycled :: Foldable f => f b -> Source b cycled = repeatedly (traverse_ yield xs)

Examples:

>>> run $ taking 5 <~ cycled [1,2]
[1,2,1,2,1]

cap :: Process a b -> Source a -> Source b Source #

You can transform a Source with a Process.

Alternately you can view this as capping the Source end of a Process, yielding a new Source.

cap l r = l <~ r

plug :: Monad m => MachineT m k o -> SourceT m o Source #

You can transform any MachineT into a SourceT, blocking its input.

This is used by capT, and capWye, and allows an efficient way to plug together machines of different input languages.

iterated :: (a -> a) -> a -> Source a Source #

iterated f x returns an infinite source of repeated applications of f to x

replicated :: Int -> a -> Source a Source #

replicated n x is a source of x emitted n time(s)

enumerateFromTo :: Enum a => a -> a -> Source a Source #

Enumerate from a value to a final value, inclusive, via succ

Examples:

>>> run $ enumerateFromTo 1 3
[1,2,3]

unfold :: (r -> Maybe (a, r)) -> r -> Source a Source #

unfold k seed The function takes the element and returns Nothing if it is done producing values or returns Just (a,r), in which case, a is yielded and r is used as the next element in a recursive call.

unfoldT :: Monad m => (r -> m (Maybe (a, r))) -> r -> SourceT m a Source #

Effectful unfold variant.