stream

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2018 License: MIT Imports: 2 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),

	"collect": wdte.GoFunc(Collect),
	"drain":   wdte.GoFunc(Drain),
	"reduce":  wdte.GoFunc(Reduce),

	"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 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 Filter

func Filter(frame wdte.Frame, args ...wdte.Func) 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) 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 Map

func Map(frame wdte.Frame, args ...wdte.Func) 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 ...

It returns a Stream that iterates over its arguments.

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).

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) 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