stream

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package stream provides WDTE functions for manipulating streams of data.

Index

Constants

This section is empty.

Variables

View Source
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),
	"skip":      wdte.GoFunc(Skip),
	"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.

Functions

func All

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

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

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

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

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 added in v0.4.0

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

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 added in v0.5.7

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

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

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 added in v0.5.4

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 added in v0.5.4

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

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

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 yields initial before it begins yielding the values returned from next. The Stream ends when next returns end.

func Range

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

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 added in v0.5.4

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 Skip added in v0.9.0

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

Skip is a WDTE function with the following signature:

(skip n) s

Skip returns a Stream that skips the first n elements of s. In other wotds, the first element of the returned stream will be element n+1 of s.

func Zip added in v0.7.1

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.

Types

type NextFunc

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

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

func (NextFunc) Next

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

func (NextFunc) Reflect added in v0.4.2

func (NextFunc) Reflect(name string) bool

func (NextFunc) String added in v0.2.3

func (NextFunc) String() string

type Stream

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL