API for stream-utils
Full namespace name: clojure.contrib.stream-utils
Functions for setting up computational pipelines via data streams.
NOTE: This library is experimental. It may change significantly
with future release.
This library defines:
- an abstract stream type, whose interface consists of the
- a macro for implementing streams
- implementations of stream for
1) Clojure sequences, and vectors
2) nil, representing an empty stream
- tools for writing stream transformers, including the
- various utility functions for working with streams
Streams are building blocks in the construction of computational
pipelines. A stream is represented by its current state plus
a function that takes a stream state and obtains the next item
in the stream as well as the new stream state. The state is
implemented as a Java class or a Clojure type (as defined by the
function clojure.core/type), and the function is provided as an
implementation of the multimethod stream-next for this class or type.
While setting up pipelines using this mechanism is somewhat more
cumbersome than using Clojure's lazy seq mechanisms, there are a
- The state of a stream can be stored in any Clojure data structure,
and the stream can be re-generated from it any number of times.
Any number of states can be stored this way.
- The elements of the stream are never cached, so keeping a reference
to a stream state does not incur an uncontrollable memory penalty.
Note that the stream mechanism is thread-safe as long as the
concrete stream implementations do not use any mutable state.
Stream transformers take any number of input streams and produce one
output stream. They are typically written using the stream-m
monad. In the definition of a stream transformer, (pick s) returns
the next value of stream argument s, whereas pick-all returns the
next value of all stream arguments in the form of a vector.
Public Variables and Functions
Usage: (defst name args streams & body)
Define the stream transformer name by body.
The non-stream arguments args and the stream arguments streams
are given separately, with args being possibly empty.
Usage: (defstream type-tag args & body)
Define object of the given type as a stream whose implementation
of stream-next is defined by args and body. This macro adds
a type-specific method for stream-next and derives type
Usage: (pick n)
Return the next value of stream argument n inside a stream
transformer. When used inside of defst, the name of the stream
argument can be used instead of its index n.
Usage: (pick-all streams)
Return a vector containing the next value of each stream argument
inside a stream transformer.
Usage: (stream-drop n stream)
Return a stream containing all but the first n elements of stream.
Usage: (stream-filter p stream)
Return a new stream that contrains the elements of stream
that satisfy the predicate p.
Usage: (stream-flatten s)
Converts a stream of sequences into a stream of the elements of the
sequences. Flattening is not recursive, only one level of nesting
will be removed.
Monad describing stream computations. The monadic values can be
of any type handled by stream-next.
Usage: (stream-map f stream)
Return a new stream by mapping the function f on the given stream.
Usage: (stream-next stream-state)
Returns a vector [next-value new-state] where next-value is the next
item in the data stream defined by stream-state and new-state
is the new state of the stream. At the end of the stream,
next-value and new-state are nil.
Usage: (stream-seq s)
Return a lazy seq on the stream. Also accessible via
clojure.contrib.generic.collection/seq for streams.
The root type for the stream hierarchy. For each stream type,
add a derivation from this type.