Safe Haskell | Safe-Inferred |
---|---|

Language | Haskell2010 |

## Synopsis

- newtype Stream m a b = Stream {}
- yield :: Monad m => a -> Stream m a ()
- liftIO :: IO a -> Stream IO b a
- collect :: Monad m => Stream m a () -> m [a]
- collect_ :: Monad m => Stream m a r -> m ([a], r)
- consume :: Monad m => Stream m a b -> (a -> m ()) -> m b
- fromList :: Monad m => [a] -> Stream m a ()
- map :: Monad m => (a -> b) -> Stream m a x -> Stream m b x
- mapM :: Monad m => (a -> m b) -> Stream m a x -> Stream m b x
- mapAccumL :: Monad m => (c -> a -> m (c, b)) -> c -> Stream m a () -> Stream m b c
- mapAccumL_ :: Monad m => (c -> a -> m (c, b)) -> c -> Stream m a r -> Stream m b (c, r)

# Documentation

`Stream m a b`

is a computation in some Monad `m`

that delivers a sequence
of elements of type `a`

followed by a result of type `b`

.

More concretely, a value of type `Stream m a b`

can be run using `runStream`

in the Monad `m`

, and it delivers either

- the final result:
`Left b`

, or `Right (a,str)`

, where`a`

is the next element in the stream, and`str`

is a computation to get the rest of the stream.

Stream is itself a Monad, and provides an operation `yield`

that
produces a new element of the stream. This makes it convenient to turn
existing monadic computations into streams.

The idea is that Stream is useful for making a monadic computation that produces values from time to time. This can be used for knitting together two complex monadic operations, so that the producer does not have to produce all its values before the consumer starts consuming them. We make the producer into a Stream, and the consumer pulls on the stream each time it wants a new value.

#### Instances

Monad m => Monad (Stream m a) Source # | |

Monad f => Functor (Stream f a) Source # | |

Monad m => Applicative (Stream m a) Source # | |

Defined in Stream pure :: a0 -> Stream m a a0 Source # (<*>) :: Stream m a (a0 -> b) -> Stream m a a0 -> Stream m a b Source # liftA2 :: (a0 -> b -> c) -> Stream m a a0 -> Stream m a b -> Stream m a c Source # (*>) :: Stream m a a0 -> Stream m a b -> Stream m a b Source # (<*) :: Stream m a a0 -> Stream m a b -> Stream m a a0 Source # |

collect :: Monad m => Stream m a () -> m [a] Source #

Turn a Stream into an ordinary list, by demanding all the elements.

collect_ :: Monad m => Stream m a r -> m ([a], r) Source #

Turn a Stream into an ordinary list, by demanding all the elements.

fromList :: Monad m => [a] -> Stream m a () Source #

Turn a list into a `Stream`

, by yielding each element in turn.

map :: Monad m => (a -> b) -> Stream m a x -> Stream m b x Source #

Apply a function to each element of a `Stream`

, lazily

mapM :: Monad m => (a -> m b) -> Stream m a x -> Stream m b x Source #

Apply a monadic operation to each element of a `Stream`

, lazily