import "github.com/keep94/gofunctional2/functional"
Package functional provides functional programming constructs.
consumers.go emitter_stream.go functional.go generators.go
var ( Done = errors.New("functional: End of Stream reached.") Skipped = errors.New("functional: Value skipped.") )
Done indicates that the end of a Stream has been reached
func EmitAll(s Stream, e Emitter) error
EmitAll emits all of Stream s to Emitter e. On success, returns nil. If the Stream for e becomes closed, EmitAll closes s and returns Done. If there was an error closing s, it returns that error.
func MultiConsume(s Stream, ptr interface{}, copier Copier, consumers ...Consumer) (closeError error)
MultiConsume consumes the values of s, a Stream of T, sending those T values to each Consumer in consumers. MultiConsume consumes values from s until no Consumer in consumers is accepting values. ptr is a *T that receives the values from s. copier is a Copier of T used to copy T values to the Streams sent to each Consumer in consumers. Passing null for copier means use simple assignment. Finally MultiConsume closes s and returns the result.
func NoCloseReader(r io.Reader) io.Reader
NoCloseReader returns an io.Reader just like r that does not implement io.Closer.
type CompositeMapper struct { // contains filtered or unexported fields }
CompositeMapper represents Mappers composed together e.g f(g(x)). Programs using CompositeMapper should typically store and pass them as values, not pointers. A CompositeMapper can be used by multiple goroutines simultaneously if its underlying Mappers can be used by multiple goroutines simultaneously. The zero value for CompositeMapper is a Mapper that maps nothing (the Map method always returns Skipped).
func Compose(f Mapper, g Mapper, c Creater) CompositeMapper
Compose composes two Mappers together into one e.g f(g(x)). If g maps type T values to type U values, and f maps type U values to type V values, then Compose returns a CompositeMapper mapping T values to V values. c is a Creater of U. Each time Map is called on returned CompositeMapper, it invokes c to create a U value to receive the intermediate result from g.
func (c CompositeMapper) Fast() Mapper
Fast returns a quicker version of this CompositeMapper that cannot be used by multiple goroutines simultaneously as if FastCompose were used.
func (c CompositeMapper) Map(srcPtr interface{}, destPtr interface{}) error
type Consumer interface { // Consume consumes values from Stream s Consume(s Stream) }
A Consumer of T consumes the T values from a Stream of T.
func ModifyConsumerStream(c Consumer, f func(s Stream) Stream) Consumer
ModifyConsumerStream returns a new Consumer that applies f to its Stream and then gives the result to c. If c is a Consumer of T and f takes a Stream of U and returns a Stream of T, then ModifyConsumerStream returns a Consumer of U.
type Copier func(src, dest interface{})
Copier of T copies the value at src to the value at dest. This type is often needed when values of type T need to be pre-initialized. src and dest are of type *T and both point to pre-initialized T.
type Creater func() interface{}
Creater of T creates a new, pre-initialized, T and returns a pointer to it.
type Emitter interface { // EmitPtr returns the pointer supplied to Next of associated Stream. // If associated Stream has been closed, EmitPtr returns nil. EmitPtr() interface{} // Return causes Next of associated Stream to return. Return yields control // to the caller of Next blocking until Next on associated Stream is called // again or Stream is closed. err is the value that Next should return. // err != functional.Done otherwise Return panics. Return(err error) }
Emitter allows a function to emit values to an associated Stream.
type Filterer interface { // Filter returns nil if value ptr points to should be included or Skipped // if value should be skipped. Filter may return other errors. ptr must be // a *T. Filter(ptr interface{}) error }
Filterer of T filters values in a Stream of T.
func All(fs ...Filterer) Filterer
All returns a Filterer that returns nil if all of the fs return nil. Otherwise it returns the first error encountered.
func Any(fs ...Filterer) Filterer
Any returns a Filterer that returns Skipped if all of the fs return Skipped. Otherwise it returns nil or the first error not equal to Skipped.
func NewFilterer(f func(ptr interface{}) error) Filterer
NewFilterer returns a new Filterer of T. f takes a *T returning nil if T value pointed to it should be included or Skipped if it should not be included. f can return other errors too.
type Mapper interface { // Map does the mapping storing the mapped value at destPtr. // If Mapper returns Skipped, then no mapped value is stored at destPtr. // Map may return other errors. srcPtr is a *T; destPtr is a *U Map(srcPtr interface{}, destPtr interface{}) error }
Mapper maps a type T value to a type U value in a Stream.
func FastCompose(f Mapper, g Mapper, ptr interface{}) Mapper
FastCompose works like Compose except that it uses a *U value instead of a Creater of U to link f ang g. ptr is the *U value. Intermediate results from g are stored at ptr. Unlike Compose, the Mapper that FastCompose returns cannot be used by multiple goroutines simultaneously since what ptr points to changes with each call to Map.
func NewMapper(m func(srcPtr interface{}, destPtr interface{}) error) Mapper
NewMapper returns a new Mapper mapping T values to U Values. In f, srcPtr is a *T and destPtr is a *U pointing to pre-allocated T and U values respectively. f returns Skipped if mapped value should be skipped. f can also return other errors.
type Rows interface { // Next advances to the next row. Next returns false if there is no next row. // Every call to Scan, even the first one, must be preceded by a call to Next. Next() bool // Reads the values out of the current row. args are pointer types. Scan(args ...interface{}) error }
Rows represents rows in a database table. Most database API already have a type that implements this interface
func NoCloseRows(r Rows) Rows
NoCloseRows returns a Rows just like r that does not implement io.Closer.
type Stream interface { // Next emits the next value in this Stream of T. // If Next returns nil, the next value is stored at ptr. // If Next returns Done, then the end of the Stream has been reached, // and the value ptr points to is unspecified. // If Next returns some other error, then the caller should close the // Stream with Close. ptr must be a *T. // Once Next returns Done, it should continue to return Done, and // Close should return nil. Next(ptr interface{}) error // Close indicates that the caller is finished with this Stream. If Caller // consumes all the values in this Stream, then it need not call Close. But // if Caller chooses not to consume the Stream entirely, it should call // Close. Caller should also call Close if Next returns an error other // than Done. Once Close returns nil, it should continue to return nil. // The result of calling Next after Close is undefined. io.Closer }
Stream is a sequence emitted values. Each call to Next() emits the next value in the stream. A Stream that emits values of type T is a Stream of T.
func Concat(s ...Stream) Stream
Concat concatenates multiple Streams into one. If x = (x1, x2, ...) and y = (y1, y2, ...) then Concat(x, y) = (x1, x2, ..., y1, y2, ...). Calling Close on returned Stream closes all underlying streams. If caller passes a slice to Concat, no copy is made of it.
func Count() Stream
Count returns an infinite Stream of int which emits all values beginning at 0.
func CountFrom(start, step int) Stream
CountFrom returns an infinite Stream of int emitting values beginning at start and increasing by step.
func Cycle(f func() Stream) Stream
Cycle returns a Stream that repeatedly calls f and emits the resulting values. Note that if f repeatedly returns the NilStream, calling Next() on returned Stream will create an infinite loop. Calling Close on returned Stream closes the last Stream f created or does nothing if f not called. If f returns a Stream of T then Cycle also returns a Stream of T.
func Deferred(f func() Stream) Stream
Deferred returns a Stream that emits the values from the Stream f returns. f is not called until the first time Next is called on the returned stream. Calling Close on returned Stream closes the Stream f creates or does nothing if f not called.
func DropWhile(f Filterer, s Stream) Stream
DropWhile returns a Stream that emits the values in s starting at the first value where the Filter method of f returns Skipped. The returned Stream's Next method reports any errors that the Filter method of f returns until it returns Skipped. Calling Close on returned Stream closes s. f is a Filterer of T; s is a Stream of T.
func Filter(f Filterer, s Stream) Stream
Filter filters values from s, returning a new Stream of T. The returned Stream's Next method reports any errors besides Skipped that the Filter method of f returns. Calling Close on returned Stream closes s. f is a Filterer of T; s is a Stream of T.
func Flatten(s Stream) Stream
Flatten converts a Stream of Stream of T into a Stream of T. Calling Close on returned Stream closes s and the last emitted Stream from s.
func Map(f Mapper, s Stream, ptr interface{}) Stream
Map applies f, which maps a type T value to a type U value, to a Stream of T producing a new Stream of U. If s is (x1, x2, x3, ...), Map returns the Stream (f(x1), f(x2), f(x3), ...). If f returns false for a T value, then the corresponding U value is left out of the returned stream. ptr is a *T providing storage for emitted values from s. Calling Close on returned Stream closes s. If f is a CompositeMapper, Fast() is called on it automatically.
func NewGenerator(f func(e Emitter)) Stream
NewGenerator creates a Stream that emits the values from emitting function f. When f is through emitting values, it should just return. If f gets nil when calling EmitPtr on e it should return immediately as this means the Stream was closed.
func NewGeneratorCloseMayFail(f func(e Emitter) error) Stream
NewGeneratorCloseMayFail creates a Stream that emits the values from emitting function f. When f is through emitting values, it should perform any necessary cleanup and return any error from the cleanup. If f gets nil when calling EmitPtr on e it should immediately perform cleanup returning any error from the cleanup as this means the Stream was closed. The Close() method on returned Stream reports any non-nil error f returns to the caller. This function is draft API and may change in incompatible ways.
func NewStreamFromPtrs(aSlice interface{}, c Copier) Stream
NewStreamFromPtrs converts a []*T into a Stream of T. aSlice is a []*T. c is a Copier of T. If c is nil, regular assignment is used. Calling Close on returned Stream does nothing.
func NewStreamFromValues(aSlice interface{}, c Copier) Stream
NewStreamFromValues converts a []T into a Stream of T. aSlice is a []T. c is a Copier of T. If c is nil, regular assignment is used. Calling Close on returned Stream does nothing.
func NilStream() Stream
NilStream returns a Stream that emits no values.
func NoCloseStream(s Stream) Stream
NoCloseStream returns a Stream just like s but with a Close method that does nothing. The returnes Stream will still automatically close itself when the end of stream is reached. This function is useful for preventing a stream from automatically closing its underlying stream.
func ReadLines(r io.Reader) Stream
ReadLines returns the lines of text in r separated by either "\n" or "\r\n" as a Stream of string. The emitted string types do not contain the end of line characters. When end of returned Stream is reached, it closes r if r implements io.Closer propagating any Close error through Next. Calling Close on returned Stream closes r if r implements io.Closer.
func ReadRows(r Rows) Stream
ReadRows returns the rows in a database table as a Stream of Tuple. When end of returned Stream is reached, it closes r if r implements io.Closer propagating any Close error through Next. Calling Close on returned stream closes r if r implements io.Closer.
func Slice(s Stream, start int, end int) Stream
Slice returns a Stream that will emit elements in s starting at index start and continuing to but not including index end. Indexes are 0 based. If end is negative, it means go to the end of s. Calling Close on returned Stream closes s. When end of returned Stream is reached, it closes s if it has not consumed s returning any Close error through Next.
func TakeWhile(f Filterer, s Stream) Stream
TakeWhile returns a Stream that emits the values in s until the Filter method of f returns Skipped. The returned Stream's Next method reports any errors besides Skipped that the Filter method of f returns. When end of returned Stream is reached, it automatically closes s if s is not exhausted. Calling Close on returned Stream closes s. f is a Filterer of T; s is a Stream of T.
type Tuple interface { // Ptrs returns a pointer to each field in the tuple. Ptrs() []interface{} }
Tuple represents a tuple of values that ReadRows emits
Package functional imports 4 packages (graph) and is imported by 4 packages. Updated 2013-08-23. Refresh now. Tools for package owners.