st

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package st provides a stream toolkit.

Example
package main

import (
	"fmt"

	"gitee.com/lite89/st"
)

func main() {
	var xs *st.Stream[float64]
	xs = st.New(1, func() *st.Stream[float64] {
		return st.Zip(xs, xs.Tail(), func(x, y float64) float64 {
			return x + y
		})
	}).Prepend(0)

	xs.Take(6).Each(func(i int, x float64) error {
		fmt.Println(i, x)
		return nil
	})
}
Output:

0 0
1 1
2 1
3 2
4 3
5 5

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrStop      = errors.New("st: stop")
	ErrEmpty     = errors.New("st: empty")
	ErrNotSingle = errors.New("st: not single")
)

Errors.

Functions

func Fold

func Fold[V, X any](xs *Stream[X], v V, f func(V, X) V) (V, error)

Fold folds the stream.

func ToDict

func ToDict[K comparable, X any](xs *Stream[X], f func(X) K) (map[K]X, error)

ToDict converts the stream to dict.

func ToError

func ToError[X any](xs *Stream[X]) error

ToError converts the stream to error.

func ToFunc

func ToFunc[X any](xs *Stream[X]) func() (X, error)

ToFunc converts the stream to func.

func ToList

func ToList[X any](xs *Stream[X]) ([]X, error)

ToList converts the stream to list.

Types

type Complex

type Complex interface {
	~complex64 | ~complex128
}

Complex is a constraint that permits any complex numeric type.

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint that permits any floating-point type.

type Integer

type Integer interface {
	Signed | Unsigned
}

Integer is a constraint that permits any integer type.

type Number

type Number interface {
	Real | Complex
}

Number is a constraint that permits any number type.

type Real

type Real interface {
	Integer | Float
}

Real is a constraint that permits any real type.

type Result

type Result[K comparable, X any] struct {
	Key    K
	Values []X
}

Result represents a group result.

func (*Result[K, X]) Avg

func (a *Result[K, X]) Avg(f func(X) float64) float64

Avg returns the avg result.

func (*Result[K, X]) Count

func (a *Result[K, X]) Count() int

Count returns the count result.

func (*Result[K, X]) Max

func (a *Result[K, X]) Max(f func(X) float64) float64

Max returns the max result.

func (*Result[K, X]) Median

func (a *Result[K, X]) Median(f func(X) float64) float64

Median returns the median result.

func (*Result[K, X]) Min

func (a *Result[K, X]) Min(f func(X) float64) float64

Min returns the min result.

func (*Result[K, X]) Std

func (a *Result[K, X]) Std(f func(X) float64) float64

Std returns the std result.

func (*Result[K, X]) Sum

func (a *Result[K, X]) Sum(f func(X) float64) float64

Sum returns the sum result.

func (*Result[K, X]) Var

func (a *Result[K, X]) Var(f func(X) float64) float64

Var returns the var result.

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed is a constraint that permits any signed integer type.

type Stream

type Stream[X any] struct {
	// contains filtered or unexported fields
}

Stream represents a stream data structure.

func Concat

func Concat[X any](xs *Stream[X], xss ...*Stream[X]) *Stream[X]

Concat concats the streams.

func CrossJoin

func CrossJoin[X, Y, Z any](xs *Stream[X], ys *Stream[Y], f func(X, Y) Z) *Stream[Z]

CrossJoin does cross join.

func Distinct

func Distinct[X comparable](xs *Stream[X]) *Stream[X]

Distinct returns a distinct stream.

func Filter

func Filter[X any](xs *Stream[X], f func(X) bool) *Stream[X]

Filter filters the elements of the stream.

func FromDict

func FromDict[K comparable, X any](dict map[K]X) *Stream[X]

FromDict creates a stream from dict.

func FromError

func FromError[X any](err error) *Stream[X]

FromError creates a stream from error.

func FromFunc

func FromFunc[X any](f func() (X, error)) *Stream[X]

FromFunc creates a stream from func.

func FromList

func FromList[X any](list []X) *Stream[X]

FromList creates a stream from list.

func Group

func Group[K comparable, X any](xs *Stream[X], f func(X) K) *Stream[*Result[K, X]]

Group groups the elements.

func LeftJoin

func LeftJoin[K comparable, X, Y, Z any](xs *Stream[X], ys *Stream[Y], xk func(X) K, yk func(Y) K, f func(K, X, Y) Z) *Stream[Z]

LeftJoin does left join.

func Make

func Make[X any](xs ...X) *Stream[X]

Make makes a stream from elements.

func Map

func Map[X, Y any](xs *Stream[X], f func(X) Y) *Stream[Y]

Map maps the elements of the stream.

func New

func New[X any](head X, builder func() *Stream[X]) *Stream[X]

New creates a new stream.

func Range

func Range[X Real](a, b, s X) *Stream[X]

Range creates a range iterator.

func Repeat

func Repeat[X any](x X) *Stream[X]

Repeat creates a cycle stream with the same element.

func RightJoin

func RightJoin[K comparable, X, Y, Z any](xs *Stream[X], ys *Stream[Y], xk func(X) K, yk func(Y) K, f func(K, X, Y) Z) *Stream[Z]

RightJoin does right join.

func Sequence

func Sequence[X Number](a, s X) *Stream[X]

Sequence creates an infinite incremental sequence.

func Zip

func Zip[X, Y, Z any](xs *Stream[X], ys *Stream[Y], f func(X, Y) Z) *Stream[Z]

Zip zips the xs and ys to zs.

func (*Stream[X]) All

func (a *Stream[X]) All(f func(X) bool) (bool, error)

All returns whether all elements match the given function.

func (*Stream[X]) Any

func (a *Stream[X]) Any(f func(X) bool) (bool, error)

Any returns whether any elements match the given function.

func (*Stream[X]) Append

func (a *Stream[X]) Append(xs ...X) *Stream[X]

Append appends elements to the stream.

func (*Stream[X]) Concat

func (a *Stream[X]) Concat(xss ...*Stream[X]) *Stream[X]

Concat concats the streams.

func (*Stream[X]) Count

func (a *Stream[X]) Count() (int, error)

Count counts the elements of the stream.

func (*Stream[X]) Drop

func (a *Stream[X]) Drop(n int) *Stream[X]

Drop drops the last n elements.

func (*Stream[X]) DropWhile

func (a *Stream[X]) DropWhile(f func(X) bool) *Stream[X]

DropWhile drops last elements of the stream as long as f returns true.

func (*Stream[X]) Each

func (a *Stream[X]) Each(f func(int, X) error) error

Each applies the given function to each element of the stream.

func (*Stream[X]) Filter

func (a *Stream[X]) Filter(f func(X) bool) *Stream[X]

Filter filters the elements of the stream.

func (*Stream[X]) Fold

func (a *Stream[X]) Fold(v X, f func(X, X) X) (X, error)

Fold folds the stream.

func (*Stream[X]) Force

func (a *Stream[X]) Force() *Stream[X]

Force calculates the elements of the stream immediately, no lazy.

func (*Stream[X]) Head

func (a *Stream[X]) Head() (X, error)

Head returns the stream head.

func (*Stream[X]) IsEmpty

func (a *Stream[X]) IsEmpty() bool

IsEmpty checks if the stream is empty or not.

func (*Stream[X]) Map

func (a *Stream[X]) Map(f func(X) X) *Stream[X]

Map maps the elements of the stream.

func (*Stream[X]) Prepend

func (a *Stream[X]) Prepend(xs ...X) *Stream[X]

Prepend prepends elements to the stream.

func (*Stream[X]) Single

func (a *Stream[X]) Single(defaults ...X) (x X, err error)

Single returns the single element.

func (*Stream[X]) Skip

func (a *Stream[X]) Skip(n int) *Stream[X]

Skip skips the first n elements.

func (*Stream[X]) SkipWhile

func (a *Stream[X]) SkipWhile(f func(X) bool) *Stream[X]

SkipWhile skips first elements of the stream as long as f returns true.

func (*Stream[X]) Sort

func (a *Stream[X]) Sort(f func(x, y X) bool) *Stream[X]

Sort sorts the stream.

func (*Stream[X]) Tail

func (a *Stream[X]) Tail() *Stream[X]

Tail returns the stream tail.

func (*Stream[X]) Take

func (a *Stream[X]) Take(n int) *Stream[X]

Take takes the first n elements.

func (*Stream[X]) TakeWhile

func (a *Stream[X]) TakeWhile(f func(X) bool) *Stream[X]

TakeWhile takes first elements of the stream as long as f returns true.

func (*Stream[X]) ToError

func (a *Stream[X]) ToError() error

ToError converts the stream to error.

func (*Stream[X]) ToFunc

func (a *Stream[X]) ToFunc() func() (X, error)

ToFunc converts the stream to func.

func (*Stream[X]) ToList

func (a *Stream[X]) ToList() ([]X, error)

ToList converts the stream to list.

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned is a constraint that permits any unsigned integer type.

Jump to

Keyboard shortcuts

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