st

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: MIT Imports: 4 Imported by: 1

README

简介

Go语言实现的stream数据类型,并提供了丰富的操作方法。

文档:pkg.go.dev/gitee.com/lite89/st

特点

  • 简单:核心代码只有十几行(Head、Tail两个方法)
  • 完整:一套完整的stream操作,此外还实现了分组、连接等操作
  • 一致:核心数据类型只有stream一个,分组、连接等操作也是返回stream
  • 泛型:采用泛型实现,编译期类型安全
  • 惰性:stream是惰性求值的,可以处理无穷序列
  • 记忆性:stream惰性求值时会记忆结果,不会重复计算
  • 不变性:由于具备记忆性,因此一个stream永远代表一个确定的序列
  • 安全性:永远不会发生空指针异常,事实上nil代表一个没有错误的空stream
  • 错误处理:能够妥善处理底层数据流的错误,如读文件/数据库发生错误

依赖

使用

构造函数

以下构造函数都是惰性求值的:

  • New是最基础的构造函数,你很少用到它
  • FromError创造一个error stream,你很少用到它
  • FromFunc从一个迭代函数创造一个stream
  • FromList从一个列表(slice)创造一个stream
  • FromDict从一个字典(map)创造一个stream
  • Make将参数列表包装成stream
  • Repeat创造一个包含重复元素的无穷序列stream
  • Sequence创造一个等差无穷序列stream
  • Range创造一个等差区间序列stream

变换操作

以下变换操作都是惰性求值的:

  • Filter执行过滤操作,并返回一个新的stream
  • Map执行映射操作,并返回一个新的stream
  • Zip将两个stream的元素对应,生成一个新的stream
  • Flat将stream元素扁平化,并返回一个新的stream
  • Concat将多个stream连接成一个新的stream
  • Append在尾部添加若干个元素,并返回一个新的stream
  • Prepend在头部添加若干个元素,并返回一个新的stream
  • TakeTakeWhile获取前面若干个元素,并返回一个新的stream
  • SkipSkipWhile忽略前面若干个元素,并返回一个新的stream
  • DropDropWhile丢弃尾部若干个元素,并返回一个新的stream

聚合操作

Group函数按照指定的键对stream中的元素进行分组,并返回分组结果,及时求值。分组后的结果本身也是一个stream,具备stream的所有特性。通常会将GroupMap等函数组合使用。分组结果提供了以下聚合函数:

  • Count返回样本个数
  • Min返回样本最小值
  • Max返回样本最大值
  • Sum返回样本求和结果
  • Avg返回样本平均值
  • Var返回样本方差
  • Std返回样本标准差
  • Median返回样本中位数

连接操作

连接操作的返回结果也是stream:

  • CrossJoin执行交叉连接操作,惰性求值
  • LeftJoin执行左连接操作,对右stream及时求值,对左stream惰性求值
  • RightJoin执行右连接操作,对左stream及时求值,对右stream惰性求值

转换操作

  • ToError返回stream中的错误,没有错误的stream返回nil,惰性求值
  • ToFunc将一个stream转化为迭代函数,惰性求值
  • ToList将一个stream转化为列表(slice),及时求值
  • ToDict将一个stream转化为字典(map),及时求值

其他操作

  • Head返回首个元素,如果是空stream(包括error stream),返回错误
  • IsEmpty判断是否是空stream,error stream也是空stream,惰性求值
  • Force强制求值,用于脱离底层数据依赖,及时求值
  • Distinct保留唯一元素,并返回一个新的stream,惰性求值
  • Sort执行排序操作,并返回一个新的stream,及时求值
  • Each执行遍历操作,及时求值
  • Count计算stream中元素个数,及时求值
  • All判断stream中是否全部元素满足条件,及时求值
  • Any判断stream中是否任意元素满足条件,及时求值
  • Single返回stream中的单一元素,如果不是单一的,则返回首个元素,并错误提示
  • Fold执行折叠操作,及时求值

注意

  • 不要对无穷序列stream执行及时求值操作
  • Filter不是严格意义上的惰性求值,事实上它会及时求值直到遇到首个符合条件的元素
  • Distinct是基于Filter实现的,也不是严格意义上的惰性求值
  • 部分操作提供函数、方法两种形式。函数形式是功能完整的,方法形式便于链式调用,但受限于Go语言不支持泛型方法

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, error) {
			return x + y, nil
		})
	}).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 Flat added in v0.2.1

func Flat[X any](xss *Stream[[]X]) *Stream[X]

Flat flats the xss to xs.

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, error)) *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, error)) *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, error)) *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, error)) *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() (head X, err 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, error)) *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