API for stream-utils
- ()
by Konrad Hinsen
Usage:
(ns your-namespace
(:require clojure.contrib.stream-utils))
Overview
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
multimethod stream-next
- 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
monad stream-m
- 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
few advantages:
- 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
defst
macro
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.
Source
defstream
macro
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
from stream-type.
Source
pick
function
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.
Source
pick-all
function
Usage: (pick-all streams)
Return a vector containing the next value of each stream argument
inside a stream transformer.
Source
stream-drop
function
Usage: (stream-drop n stream)
Return a stream containing all but the first n elements of stream.
Source
stream-filter
multimethod
Usage: (stream-filter p stream)
Return a new stream that contrains the elements of stream
that satisfy the predicate p.
Source
stream-flatten
function
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.
Source
stream-m
var
Monad describing stream computations. The monadic values can be
of any type handled by stream-next.
Source
stream-map
multimethod
Usage: (stream-map f stream)
Return a new stream by mapping the function f on the given stream.
Source
stream-next
multimethod
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.
Source
stream-seq
function
Usage: (stream-seq s)
Return a lazy seq on the stream. Also accessible via
clojure.contrib.seq/seq-on and
clojure.contrib.generic.collection/seq for streams.
Source
stream-type
var
The root type for the stream hierarchy. For each stream type,
add a derivation from this type.
Source