wdte: github.com/DeedleFake/wdte/std/stream Index | Files

package stream

import "github.com/DeedleFake/wdte/std/stream"

Package stream provides WDTE functions for manipulating streams of data.

Index

Package Files

end.go middle.go start.go stream.go

Variables

var Scope = wdte.S().Map(map[wdte.ID]wdte.Func{
    "new":    wdte.GoFunc(New),
    "range":  wdte.GoFunc(Range),
    "concat": wdte.GoFunc(Concat),

    "map":       wdte.GoFunc(Map),
    "filter":    wdte.GoFunc(Filter),
    "flatMap":   wdte.GoFunc(FlatMap),
    "enumerate": wdte.GoFunc(Enumerate),
    "repeat":    wdte.GoFunc(Repeat),
    "limit":     wdte.GoFunc(Limit),
    "zip":       wdte.GoFunc(Zip),

    "end":     End(),
    "collect": wdte.GoFunc(Collect),
    "drain":   wdte.GoFunc(Drain),
    "reduce":  wdte.GoFunc(Reduce),
    "fold":    wdte.GoFunc(Fold),
    "extent":  wdte.GoFunc(Extent),

    "any": wdte.GoFunc(Any),
    "all": wdte.GoFunc(All),
})

Scope is a scope containing the functions in this package.

func All Uses

func All(frame wdte.Frame, args ...wdte.Func) wdte.Func

All is a WDTE function with the following signatures:

all s f
(all f) s

It iterates over the Stream s, passing each yielded element to f in turn. If all of those calls return true, then the entire function returns true. Otherwise it returns false. It is short-circuiting.

func Any Uses

func Any(frame wdte.Frame, args ...wdte.Func) wdte.Func

Any is a WDTE function with the following signatures:

any s f
(any f) s

It iterates over the Stream s, passing each yielded element to f in turn. If any of those calls returns true, then the entire function returns true. Otherwise it returns false. It is short-circuiting.

func Collect Uses

func Collect(frame wdte.Frame, args ...wdte.Func) wdte.Func

Collect is a WDTE function with the following signature:

collect s

Iterates through the Stream s, collecting the yielded elements into an array. When the Stream ends, it returns the collected array.

func Concat Uses

func Concat(frame wdte.Frame, args ...wdte.Func) wdte.Func

Concat is a WDTE function with the following signatures:

concat s ...
(concat s) ...

It returns a new Stream that yields the values of all of its argument Streams in the order that they were given.

func Drain Uses

func Drain(frame wdte.Frame, args ...wdte.Func) wdte.Func

Drain is a WDTE function with the following signature:

drain s

Drain is the same as Collect, but it simply discards elements as they are yielded by the Stream, returning the empty Stream when it's done. The main purpose of this function is to allow Map to be used as a foreach-style loop without the allocation that Collect performs.

func End Uses

func End() wdte.Func

End returns a special value that is returned by the next function provided to new when it wants to end the stream.

func Enumerate Uses

func Enumerate(frame wdte.Frame, args ...wdte.Func) wdte.Func

Enumerate is a WDTE function with the following signature:

enumerate s

It returns a Stream which yields values of the form [i; v] where i is the zero-based index of the element v that was yielded by the Stream s.

func Extent Uses

func Extent(frame wdte.Frame, args ...wdte.Func) wdte.Func

Extent is a WDTE function with the following signatures:

extent s n less
(extent n less) s
(extent less) s n
((extent less) n) s

It drains the Stream s, building up a list of up to n elements yielded for which less returns true compared to other elements in the list, sorted such that the first element of the list is the most less of them. In other words, it returns the n most minimum elements using less to perform the compartison. For example,

range 10 -> extent 3 >

will return [9; 8; 7].

If n is less than 0, there is no limit on the length of the list built, meaning that it will contain every element that the Stream yields, essentially acting like a sorting variant of collect.

func Filter Uses

func Filter(frame wdte.Frame, args ...wdte.Func) (filter wdte.Func)

Filter is a WDTE function with the following signature:

(filter f) s

It returns a Stream which yields only those values yielded by the Stream s that (f value) results in true for.

func FlatMap Uses

func FlatMap(frame wdte.Frame, args ...wdte.Func) (mapper wdte.Func)

FlatMap is a WDTE function with the following signature:

(flatMap f) s

It's identical to Map with one caveat: If a call to f yields a Stream, the elements of that Stream are yielded in turn before continuing the iteration of s. In other words,

range 3 -> flatMap (new 0 1) -> collect

returns

[0; 1; 0; 1; 0; 1]

func Fold Uses

func Fold(frame wdte.Frame, args ...wdte.Func) wdte.Func

Fold is a WDTE function with the following signatures:

fold s r
(fold r) s

Fold is exactly like Reduce, but is uses the first element of the Stream s as its initial element, rather than taking an explicit one. If there is no first element, it returns End.

func Limit Uses

func Limit(frame wdte.Frame, args ...wdte.Func) wdte.Func

Limit is a WDTE function with the following signature:

(limit n) s

Limit returns a Stream that stops after a maximum of n elements from s have been yielded.

func Map Uses

func Map(frame wdte.Frame, args ...wdte.Func) (mapper wdte.Func)

Map is a WDTE function with the following signature:

(map f) s

It returns a Stream which calls f on each element yielded by the Stream s, yielding the return values of f in their place.

func New Uses

func New(frame wdte.Frame, args ...wdte.Func) wdte.Func

New is a WDTE function with the following signature:

new initial next
(new next) initial

It returns a new Stream that calls next in order to get the next element in the stream, passing it first initial and then the previous value on each call. The Stream ends when next returns end.

func Range Uses

func Range(frame wdte.Frame, args ...wdte.Func) wdte.Func

Range is a WDTE function with the following signatures:

range end
range start end
range start end step

It returns a new Stream which iterates from start to end, stepping by step each time. In other words, it's similar to the following pseudo Go code

for i := start; i < end; i += step {
  yield i
}

but with the difference that if step is negative, then the loop condition is inverted.

If start is not specified, it is assumed to be 0. If step is not specified it is assumed to be 1 if start is greater than or equal to end, and -1 if start is less then end.

func Reduce Uses

func Reduce(frame wdte.Frame, args ...wdte.Func) wdte.Func

Reduce is a WDTE function with the following signatures:

reduce s i r
(reduce r) s i
(reduce i r) s

Reduce performs a reduction on the Stream s, resulting in a single value, which is returned. i is the initial value for the reduction, and r is the reducer. r is expected to have the following signature:

r acc n

r is passed the accumulated value as acc, starting with i, and the latest value yielded by the Stream as n. Whatever value r returns is used as the next value of acc until the Stream is empty, at which point the last value of acc is returned. For example,

range 5 -> reduce 0 +

returns a summation of the range [0,5).

func Repeat Uses

func Repeat(frame wdte.Frame, args ...wdte.Func) wdte.Func

Repeat is a WDTE function with the following signature:

repeat s

Repeat returns a Stream that buffers the elements from the Stream s. Once s has ended, the Stream starts repeating from the begnning of the buffer, looping infinitely. In other words,

range 3 -> repeat

will yield the sequence (0, 1, 2) repeatedly with no end.

Repeat is most useful used with Limit. When combining the two, note that Limit limits individual elements, not repetitions, so the number passed to Limit should be multiplied properly if the client wants to limit to a specific number of repetitions.

func Zip Uses

func Zip(frame wdte.Frame, args ...wdte.Func) wdte.Func

Zip is a WDTE function with the following signatures:

(zip s1) ...
zip ...

Zip returns a Stream which yields the streams that it is given simultaneuously as arrays. In other words,

zip (a.stream [1; 2; 3]) (a.stream ['a'; 'b'; 'c'])

will yield

[1; 'a']
[2; 'b']
[3; 'c']

The order of the yielded arrays matches the order that the streams are given in. If one of the streams ends before the other ones, End will be yielded for that stream after that point.

type NextFunc Uses

type NextFunc func(frame wdte.Frame) (wdte.Func, bool)

A NextFunc wraps a Go function, making it possible to use it as a Stream. When called as a WDTE function, the function simply returns itself.

func (NextFunc) Call Uses

func (n NextFunc) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func

func (NextFunc) Next Uses

func (n NextFunc) Next(frame wdte.Frame) (wdte.Func, bool)

func (NextFunc) Reflect Uses

func (NextFunc) Reflect(name string) bool

func (NextFunc) String Uses

func (NextFunc) String() string

type Stream Uses

type Stream interface {
    wdte.Func

    // Next returns the next value and true, or an undefined value and
    // false if the stream is empty.
    Next(frame wdte.Frame) (wdte.Func, bool)
}

A Stream is a type of function that can yield successive values.

Package stream imports 4 packages (graph) and is imported by 2 packages. Updated 2019-07-21. Refresh now. Tools for package owners.